Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  61] [ 4]  / answers: 1 / hits: 10498  / 11 Years ago, fri, january 24, 2014, 12:00:00

A controller has $http that calls an api backend on Flask. I have some basic authentication and crossdomain is set. The first time it enters the cpuListCtrl controller the $http calls takes cca. ~14sec. The next time i visited the controller in angular it takes just 23ms. But every time i press the browsers refresh, back to ~14sec. Direct api call from browser also takes just 23ms. So my question is my does it takes so long, did i miss something, or where specific should i look?



EDIT: updated the code to reflect recent changes:



  var app = angular.module('RecycleApp', ['ngRoute', 'appControllers']);
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);

app.config(['$routeProvider', function($routeProvider){
$routeProvider
.when(/cpu, {
templateUrl:'static/js/partials/cpu.html',
controller:'cpuCtrl'
})
}]);

var appControllers = angular.module('appControllers', []);

appControllers.controller('cpuCtrl', ['$scope','$http',
function($scope,$http){
$http({
url: 'http://SOME_IP/api/v1/cpus',
method: 'POST',
data: JSON.stringify({latitude:46.1948436, longitude:15.2000873}),
headers: {Content-Type:application/json}
})
.success(function(data,status,headers,config){
console.log(data.list);
$scope.cpus = data.list;
})
.error(function(data,status,headers,config){
console.log(something went wrong.);
})

}]);


Server side:



@app.route('/api/v1/cpus', methods=[GET])
@cross_origin(origins='*', headers=(Content-Type))
def get_cpu_list():
result = session.query(RecycleReUseCenter)
.options(load_only(Id, CpuName))
.all()
return list_json(result)

@app.route(/api/v1/cpus, methods=[POST])
@cross_origin(origins='*', headers=(Content-Type))
def get_cpu_list_with_locations():
content = request.get_json(force=True)
given_latitude = content['latitude']
given_longitude = content['longitude']

result = RecycleReUseCenter.get_all_with_distance(given_latitude, given_longitude)
return list_json(result)

More From » angularjs

 Answers
8

Do you know for sure when does the http call starts? The angular app may be stuck somewhere else, and getting to the http call only in the last second. For example - in the config you are using a token where do you get it from? in many angular app this is fetched from some oauth service, in a seperete call. Your slow call won't start until http is configured. After token is there, the next calls would be faster since we got the token already.



To limit some guessing you can use a proxy tool like charles - or deflect.io chrome extension to watch all the out going http calls and figure this out


[#48374] Thursday, January 23, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emileef

Total Points: 724
Total Questions: 108
Total Answers: 102

Location: Romania
Member since Sun, Dec 20, 2020
3 Years ago
;