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

    评论

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图