jQuery Ajax 404 处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4281274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:58:56  来源:igfitidea点击:

jQuery Ajax 404 Handling

jquery

提问by Lee

Im trying to access a 404 event which I can see coming back as 404 with firebug but the error function is not kicking in, With my below code I always get Error: success ?.

我试图访问一个 404 事件,我可以看到它作为 404 与 firebug 一起返回,但错误功能没有启动,使用我下面的代码,我总是得到错误:成功?。

ie.

IE。

$.ajax({
    type: 'get',
    url: 'url: 'https://admin.instantservice.com/resources/smartbutton/5702/10945/available.gif?' + Math.floor(Math.random()*10001),
    success: function(data, textStatus, XMLHttpRequest){
        console.log('Error: ' + textStatus);
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(xhr.statusText);
        alert(xhr.responseText);
    }
});

Again I know 1000% that im getting 404 Not Found in firebug it never triggers the error.

我再次知道 1000% 我在萤火虫中找不到 404 它永远不会触发错误。

Am I missing something ?

我错过了什么吗?

采纳答案by firecall

Cross Site requests, JSONP, wont trigger the error calls. Just had the same problem myself: http://forum.jquery.com/topic/jquery-ajax-with-datatype-jsonp-will-not-use-error-callback-if-request-fails

跨站点请求,JSONP,不会触发错误调用。我自己也遇到了同样的问题:http: //forum.jquery.com/topic/jquery-ajax-with-datatype-jsonp-will-not-use-error-callback-if-request-fails

回答by Fergal Moran

What you can also do is use the $.ajaxError function, like so

你还可以做的是使用 $.ajaxError 函数,就像这样

$("#message").ajaxError(function (event, request, settings) {
    $(this).show();
    $(this).append("<li>Error requesting page " + settings.url + "</li>");
});

回答by amurrell

I had a very similar result/problem. The bottom line is that I failed to make sure that my URL did not contain any of the same param names in the data sectionof the ajax function.

我有一个非常相似的结果/问题。最重要的是,我未能确保我的 URL在 ajax 函数的数据部分不包含任何相同的参数名称



The reason for me, was that I generated the URL with php code to get a url

对我来说,原因是我用php代码生成了网址来获取网址

pines_url('com_referral', 'sendhttprequest')

which outputs to a url with a parameter

输出到带有参数的 url

option=com_referral

and a parameter

和一个参数

action=sendhttprequest

My BIG problem was that I was also trying to send a param "action" in the data section of an AJAX function.

我的大问题是我还试图在 AJAX 函数的数据部分发送一个参数“动作”。

data: {"action": "getpoints"}

So the second action was overriding it "getpoints" does not exist, but the tricky part is that firebug would obviously not tell me the url with getpoints, it would tell me the url as sendhttprequest.

所以第二个动作是覆盖它“getpoints”不存在,但棘手的部分是firebug显然不会告诉我带有getpoints的url,它会告诉我url为sendhttprequest。

Hope that wasn't confusing, but I also hope this helps someone else with a similar problem!

希望这不会令人困惑,但我也希望这可以帮助遇到类似问题的其他人!

回答by uberweb

Adding a timeout value will cause the error callback to run if there is no response in the specified time. With a callback of 5000, if the jsonp request doesn't respond in 5 seconds (i.e. it 404s) then the error callback will run.

如果在指定时间内没有响应,添加超时值将导致错误回调运行。回调为 5000,如果 jsonp 请求在 5 秒内没有响应(即 404 秒),则错误回调将运行。

$.ajax({
    type: 'get',
    timeout: 5000,
    url: 'url: 'https://admin.instantservice.com/resources/smartbutton/5702/10945/available.gif?' + Math.floor(Math.random()*10001),
    success: function(data, textStatus, XMLHttpRequest){
        console.log('Error: ' + textStatus);
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(xhr.statusText);
        alert(xhr.responseText);
    }
});

回答by neves

I don't know exactly why, but if you use the chaining .failmethod, the error is correctly captured even if it is a different site:

我不知道确切的原因,但是如果您使用链接.fail方法,即使它是不同的站点,也会正确捕获错误:

$.ajax({
    type: 'get',
    url: 'http://example.com',
    success: function(data, textStatus, XMLHttpRequest){
        console.log('Error: ' + textStatus);
      }
    }).fail(function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(xhr.statusText);
        alert(xhr.responseText);
    });

回答by Among Amrul

Remove 'url: ' in your code

删除代码中的 'url:'

Your code :

你的代码:

$.ajax({
    type: 'get',
    url: 'url: 'https://admin.instantservice.com/resources/smartbutton/5702/10945/available.gif?' + Math.floor(Math.random()*10001),
    success: function(data, textStatus, XMLHttpRequest){
        console.log('Error: ' + textStatus);
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(xhr.statusText);
        alert(xhr.responseText);
    }
});

Correct code :

正确的代码:

$.ajax({
    type: 'get',
    url: 'https://admin.instantservice.com/resources/smartbutton/5702/10945/available.gif?' + Math.floor(Math.random()*10001),
    success: function(data, textStatus, XMLHttpRequest){
        console.log('Error: ' + textStatus);
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(xhr.statusText);
        alert(xhr.responseText);
    }
});