jQuery的阿贾克斯多个呼叫 [英] jquery-ajax multiple calls

查看:120
本文介绍了jQuery的阿贾克斯多个呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code有这样多的Ajax请求:

I'm using the following code to have multiple ajax requests like this:

要求 1开始 |要求 1完成 |要求 2开始 |要求 2完成 | ......

request 1 start | request 1 finish | request 2 start | request 2 finish | ...

这是在code:

var startingpoint = fireRequest(1);
    $.each(types,function(ix,type) 
    {
       startingpoint = startingpoint.pipe( function() 
       {
          alert(startingpoint.status); // undefined
          return fireRequest(type);
        });
    });

fireRequest 只是一个switchcase正确的ajax函数返回$阿贾克斯(...)

fireRequest is just a switchcase to the correct ajax function which returns $.ajax(...)

我想链条,当一个请求无法停止。我开始实施,并作为一个测试,我想提醒AJAX对象的状态,但它显示未定义。我怎样才能得到的地位?

I'd like the chain to stop when one request fails. I started implementing it and as a test I'd like to alert the status of the ajax object, but it shows 'undefined'. How can I get the status?

推荐答案

正在试图执行的行为已经是.pipe()方法的行为。它需要两个回调作为参数,只会执行完成回调,并沿着供应链继续,如果previous请求成功。这可说明在下面的jsfiddle: http://jsfiddle.net/dflor003/Vq2YF/ (注:在具有浏览器中打开该内置JSON.stringify支持和执行console.log支持)

The behavior you are trying to implement is already the behavior of the .pipe() method. It takes two callbacks as parameters and will ONLY execute the done callback and continue along the chain if the previous request succeeds. This can be illustrated in the following jsfiddle: http://jsfiddle.net/dflor003/Vq2YF/ (Note: open this in a browser that has built-in JSON.stringify support and console.log support)

如果你想检查请求的状态,将采取的状态作为第二个参数进行回调。更多详细信息可以在jQuery的API文档网站上找到: http://api.jquery.com/deferred.pipe /

If you do want to check the status of a request, it will take the status as the second parameter to the done callback. More detail can be found on jQuery's API documentation site: http://api.jquery.com/deferred.pipe/

这篇关于jQuery的阿贾克斯多个呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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