weixin_33709364 2014-01-31 21:37 采纳率: 0%
浏览 15

Ajax超时只工作一次

I am coding a page that makes two AJAX requests by using jQuery, each one to a different server. But the problem is when each requests needs to call to it's own timeout event. It seems that the only timeout event it's fired by the last AJAX request made. If I just do a single request on the page the timeout works, but if I add a second request the first script's timeout does not work.

I already spent hours searchig about how to fix this with out success.

Here is an example of my code to help you figure out what I am talking about:

$(document).ready(function($) { // This goes in the fist script but I could not put the script tags
    function getData() {
        $.ajax({
        url: "urlpath",
        async: false,
        dataType: "jsonp",
        timeout: 4000,  // This timeout does not work when the second request is present
            success: function(parsed_json) {
                console.log("Success for the fist request");
            },
            error: function(request, status, err) {
            if (status == "timeout") {
                console.log("Timeout error for the first request");
            } else {
                console.log("Unknown error for the fist request");
            }
            }
        });
    });
$(document).ready(function() { // This goes in the second script
    $.ajax({
    url : "urlpath.json",
    dataType : "json",
    timeout: 8000, // This timeout does work
    success: function(parsed_json) {
    console.log(Success for the last request);
    },
    error: function(request, status, err) {
        if (status == "timeout") {
            console.log(Timeout reached for the last request);
        } else {
            console.log(Unknown error on the last request);
            }
        }
    });
});

Any help is welcomed. Thank you so much in advance.

  • 写回答

1条回答 默认 最新

  • weixin_33720452 2014-02-01 00:03
    关注

    Your first request is jsonp, not json, so you'll probably need to handle the timeout yourself: jQuery ajax (jsonp) ignores a timeout and doesn't fire the error event

    评论

报告相同问题?