简体   繁体   English

JSONP ajax请求和jQuery.when()

[英]JSONP ajax request and jQuery.when()

I have to make a bunch of JSONP calls, and execute some code after they all finish (or fail). 我必须进行一堆JSONP调用,并在它们全部完成(或失败)之后执行一些代码。

ie: 即:

function finished() {
    alert('done!');
}

function method1() {
    return $.ajax('http://example.org/endpoint', {
        data: {
            foo: 'bar'
        },
        dataType: 'jsonp'
    });
}

function method2() {
    return $.ajax('http://example.org/endpoint', {
        data: {
            baz: 'qux'
        },
        dataType: 'jsonp'
    });
}

$.when(method1(), method2()).always(finished);

The only problem is, if any of the requests fail, finished() will not be called. 唯一的问题是,如果任何请求失败,将不会调用finished()

I can try and detect if one of the ajax calls fails like so: 我可以尝试检测ajax调用是否失败,如下所示:

function method1() {
    var method1_timeout = setTimeout(function() {
        // this call has failed - do something!
    }, 5000);

    var ajax = $.ajax('http://example.org/endpoint', {
        data: {
            foo: 'bar'
        },
        dataType: 'jsonp',
        success: function() {
            clearTimeout(method1_timeout);
        }
    });

    return ajax;
}

But I'm stuck at that point - how do I tell the deferred object that that particular ajax request has failed? 但是我被困在这一点上-如何告诉延迟的对象那个特定的ajax请求失败了?

I tried calling the ajax object's error() method: 我尝试调用ajax对象的error()方法:

var method1_timeout = setTimeout(function() {
    ajax.error();
}, 5000);

But no luck. 但是没有运气。

Any ideas? 有任何想法吗?

I tested your posted code 'as-is' in jsFiddle and it works, even when the JSONP calls fails (of course, requests returns 404 errors because of example.com/endpoint queries). 我在jsFiddle中测试了您发布的代码“按原样”并且即使JSONP调用失败也可以正常工作(当然,由于example.com/endpoint查询,请求返回404错误)。

So I think I found the answer of your problem in the jQuery documentation, in the $.ajax chapter: 所以我想我在$.ajax章节的jQuery文档中找到了您问题的答案:

The server should return valid JavaScript that passes the JSON response into the callback function. 服务器应返回将JSON响应传递到回调函数中的有效JavaScript $.ajax() will execute the returned JavaScript, calling the JSONP callback function, before passing the JSON object contained in the response to the $.ajax() success handler. $ .ajax()将执行返回的JavaScript,并调用JSONP回调函数,然后将响应中包含的JSON对象传递给$ .ajax()成功处理程序。

Check if your JSONP returned JS code are valid and check if you don't have any syntax error in your console that will stop the execution of your JS code on your side (and prevent the always function to be executed). 检查您的JSONP返回的JS代码是否有效,并检查您的控制台中是否没有语法错误,该错误会停止您这边的JS代码执行(并阻止always执行功能)。

EDIT: I reproduced your error. 编辑:我转载了您的错误。 When using jQuery 2.x (edge) , it works like intended ( try there ). 当使用jQuery 2.x(edge)时 ,它的工作原理与预期的相同(请尝试 )。 Passing to jQuery 1.x (edge) , the two methods calls are working but the always func is never called ( try there ). 传递给jQuery 1.x(edge)时 ,这两种方法都可以正常工作,但always不会调用函数func( 在那儿尝试 )。 All my tests have been done under Google Chrome . 我所有的测试都是在Google Chrome浏览器下完成的。

setTimeouts are in the global scope in JS and you created the variable ajax inside your function. setTimeouts在JS的全局范围内,并且您在函数内部创建了变量ajax。 Try 尝试

var method1_timeout = function(){ ajax.error();}

inside the function in which you defined var ajax and then provide method1_timeout to your setTimeout as a callback. 在定义var ajax的函数内部,然后将method1_timeout提供给setTimeout作为回调。

setTimeout(method1_timeout, 5000);

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

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