Angular - $ http first call - super slow

The controller has $ http, which calls the api server on Flask. I have basic authentication and cross-domain. When you first enter the cpuListCtrl controller, calls to $ http take cca. ~ 14sec. The next time I visited the controller in angular, it took only 23 ms. But every time I click on browser updates, I return to ~ 14 seconds. A direct api call from the browser also takes only 23 ms. So my question is mine, it took so long, did I miss something, or where exactly should I look?

EDIT: Updated 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)
+4
source share
3 answers

, HTTP-? angular http- . - "", ? angular oauth seperete. , http. , , .

, -, , charles - deflect.io chrome extension, http- .

0

, , , -, Flask, Angular, Chrome. python stackexchange , - fooobar.com/questions/381241/... - "", !

0

, . :

, IPv6 IPv4. /etc/hosts apache .

0

Source: https://habr.com/ru/post/1523576/


All Articles