同时ajax请求angularjs? [英] simultaneous ajax requests angularjs?

查看:85
本文介绍了同时ajax请求angularjs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一次发送multipal ajax请求.这是我的js代码.

i want to send multipal ajax request at a time. this is my js code.

<a class='btn btn-success' ng-click='getDataajax()'>Re Check</a>

app.controller('customersCrtl', function($scope, $http, $timeout) {
    function wrapper() {
        $scope.getEventSeconds();
        $timeout(wrapper, 5000);
    }
    $http.get('cget.php', {
        params: {
            'action': 'get_info'
        }
    }).success(function(data) {
        $scope.list = data;
        $scope.currentPage = 1; //current page
        $scope.entryLimit = 10; //max no of items to display in a page
        $scope.filteredItems = $scope.list.length; //Initially for no filter  
        $scope.totalItems = $scope.list.length;
    });
    $timeout(wrapper, 5000);
    $scope.getDataajax = function() {
        $http.get('cget.php', {
            params: {
                'action': 'set_info'
            }
        }).success(function(data) {});
    };
    $scope.getEventSeconds = function() {
        $http.get('cget.php', {
            params: {
                'action': 'get_info'
            }
        }).success(function(data) {
            $scope.list = data;
            $scope.currentPage = 1; //current page
            $scope.entryLimit = 10; //max no of items to display in a page
            $scope.filteredItems = $scope.list.length; //Initially for no filter  
            $scope.totalItems = $scope.list.length;
        });
    };
})

这是正常工作,发现get_info ajax每5秒发送一次.但是当我触发set_info ajax,然后触发get_info ajax时,它正在等待完成set_info ajax.

This is working find get_info ajax is send in every 5 second. but when i fire set_info ajax and then the get_info ajax is fire it's waiting for finish set_info ajax.

我想在get_info触发时不等待两个链接,直到set_info ajax完成.

i want to working both link at a time when get_info fire then is not waiting untill the set_info ajax is finished.

谢谢.

推荐答案

我认为您感兴趣的是

I think what you're interested in is $q.all. From the documentation:

all(promises);

将多个promise合并为一个promise,当所有输入promise都已解决时, 参数

all(promises);

Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.
Parameters

Param    | Type                             | Details
promises | Array.<Promise> Object.<Promise> | //An array or hash of promises

返回一个单独的Promise,它将使用值的数组/哈希值进行解析,每个值对应于promises数组/哈希值中相同索引/键处的promise.如果任何一个承诺都被拒绝解决,则所产生的承诺将以相同的拒绝值被拒绝.

Returns a single Promise that will be resolved with an array/hash of values, each value corresponding to the promise at the same index/key in the promises array/hash. If any of the promises is resolved with a rejection, this resulting promise will be rejected with the same rejection value.

$q.all({
  users: $http.get('https://api.github.com/users'),
  repos: $http.get('https://api.github.com/repositories')
}).then(function(results) {
  var users = results.users.data;
  var repos = results.repos.data;
});

这是正在执行的 plunker .

再读了几次这个问题之后,我意识到异步地发出两个ajax请求,而只想对两个请求进行单个回调不是原来的问题.

After rereading the issue a few more times, I realized that making two ajax requests asynchronously while wanting to only have a single callback for both was not the issue of the original question.

根据我的理解,问题是询问如何同时发生多个ajax请求,而不考虑先发生什么,并且不需要常见的已解决的promise回调(作为我的原始答案地址).

From my understanding, the question is asking how to have multiple ajax requests happen simultaneously, with no regards to which happens first, and no need for a common resolved promise callback (as my original answer addresses).

发出ajax请求(例如$http.get(...);)时,javascript的执行不会停止.如果发出了另一个ajax请求,则无论第一个请求是否完成,它都会触发.在间隔循环中不断发生ajax请求,并触发按钮事件时,独立的ajax请求不会引起任何冲突.

When an ajax request is made (e.g. $http.get(...);), execution of javascript won't stop. If another ajax request is made, it will fire regardless of whether or not the first one finished or not. Having an ajax request continually occurring in an interval loop and having a button event fire an independent ajax request shouldn't cause any conflict.

尽管问题中的代码并不理想,但我无法确切地了解为什么"get_info"请求正在等待"set_info"请求完成.我创建了一个jsFiddle尝试重新创建原始代码的功能,并且它似乎按预期工作:

Although the code in the question isn't ideal, I can't exactly see why the 'get_info' request is waiting for the 'set_info' request to complete. I've created a jsFiddle to try and recreate the original code's functionality and it seems to be working as expected:

http://jsfiddle.net/Tnt56/

我使用jsFiddle是因为它具有/echo/json/ api功能来测试ajax功能.如果您在控制台上打开开发人员工具,则该提琴将记录ajax请求的关键点. $ timeout包装器每5秒触发一次,我将GetDataajax的响应设置为延迟6秒.当getEventSeconds函数记录在控制台中时,单击GetDataajax按钮.它将记录它已启动,然后getEventSeconds将再次记录,最后GetDataajax将得到解决.

I used jsFiddle since it has an /echo/json/ api feature to test ajax functionality. If you open your developer tools to the console, this fiddle will log key points of the ajax requests. The $timeout wrapper fires every 5 seconds and I've set the GetDataajax's response to be delayed by 6 seconds. When the getEventSeconds function gets logged in the console, click the GetDataajax button. It will log that it started, then the getEventSeconds will log again, and finally the GetDataajax will get resolved.

另一方面,我不得不使用jQuery的$ .ajax()而不是Angular的$ http,因为出于某种原因,尽管使用了请求标头,但$ http的发布在jsFiddle的echo服务中表现不佳(内容-类型).请求有效",但延迟功能无效.

On a side note, I had to use jQuery's $.ajax() instead of Angular's $http because for some reason, the $http posting wasn't playing nice with jsFiddle's echo service, despite playing with the request headers (Content-Type). The request 'works' but the delay feature wouldn't.

这篇关于同时ajax请求angularjs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆