简体   繁体   English

使用AngularJS进行异步调用

[英]Async calls using AngularJS

We are making multiple HTTP requests using Angular: 我们正在使用Angular发出多个HTTP请求:

$scope.GetTest1 = function () {
    $http.get("/test/GetTest1/").success(function (response) {
        $scope.res = response.aaData;
    });
} 

$scope.GetTest2 = function () {
    $http.get("/test/GetTest2/").success(function (response) { 
        $scope.res = response.aaData; 
    });
}

$scope.GetTest3 = function () {
    $http.get("/test/GetTest3/").success(function (response) { 
        $scope.res = response.aaData; 
    });
}

// This is called from an onclick of a button
$scope.LoadAll = function () {
    $scope.GetTest1();
    $scope.GetTest2();
    $scope.GetTest3();
}

We assumed that these were all called async, however, we have log4net enabled and we log the datetime when the 'gets' are received, and the times for all 3 are: 我们假设所有这些都称为异步,但是,我们启用了log4net并记录了接收到“ gets”的日期时间,而这三个时间均为:

19:05:26 19时05分26秒

19:05:27 19时05分27秒

19:05:28 19时05分28秒

This was an unexpected surprise as we assumed the time would all be within 1 second. 这是一个意外的惊喜,因为我们认为时间都在1秒以内。 ie async. 即异步。

Not sure if we're missing something, 不知道我们是否缺少什么,

Sorry, question is, how do we make these async calls? 抱歉,问题是,我们如何进行这些异步调用?

I suppose that the reason of that perhaps is on the server side. 我想可能是在服务器端。 I had almost the same result when server could serve only one request from one client. 当服务器只能处理来自一个客户端的一个请求时,我得到的结果几乎相同。 If response from server fulfils your $http requests in one second then that could be a problem. 如果来自服务器的响应在一秒钟内满足了您的$http请求,则可能是一个问题。 Please check your network statistics and if you see that they were called simultaneously but were served not immediately then it's server side problem. 请检查您的网络统计信息,如果您看到它们同时被调用但没有立即得到服务,则这是服务器端问题。

You can easily track this on browser's devtools' timeline 您可以在浏览器的devtools的时间轴上轻松跟踪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM