jQuery Ajax 404处理

27

我正在尝试访问一个404事件,用Firebug可以看到它返回404,但错误函数没有启动。以下是我的代码,但我总是得到"Error: success ?"。

例如:

$.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);
    }
});

我很确定我遇到了404 Not Found错误,但在Firebug中它从未触发该错误。

我是否漏掉了什么?


我测试了完全相同的代码,并正确收到了3个错误警报(第一个是 404)。 - cambraca
你确定你实际上得到的是状态码404的响应,而不仅仅是一个带有状态码200和一个包含文字说明是404的页面的常规响应吗? - Guffa
@cambraca 我这里也一样。已经使用Firefox 3.6.12和Firebug测试过了。 - aefxx
下次调试时请使用 console.log(),它可以避免 JavaScript 锁死并且不会过于突兀。你可以在 JavaScript 控制台中查看输出结果。 - Fred
现在我已经输入了我想要尝试的URL。请尝试一下。 - Lee
6个回答

24

2
至于在问题中未被识别的非 JSONP 请求呢? - Mike Gleason jr Couturier
1
请注意,当您指定超时时间(以毫秒为单位)时,将调用错误。 - Jouke Waleson
尽管$.getJSON方法可以使用.error()方法捕获404错误,但是…… - WoodyDRN

14

你还可以使用 $.ajaxError 函数,像这样:

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

3
不确定为什么这个没有更多的赞,它完美地运作,可以看:http://api.jquery.com/ajaxError/ - cnizzardini
感谢 @systematical - 我自己也不确定,但对我来说非常有效,我在所有网站的样板中都有它。 - Fergal Moran
7
这个对我没有帮助的原因是因为它仍然无法在JSONP的情况下被调用。来自http://api.jquery.com/ajaxError/的说明:"注意:对于跨域脚本和跨域JSONP请求,此处理程序不会被调用。" - jacobq

1

添加超时值将导致错误回调在指定时间内没有响应时运行。使用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);
    }
});

1
这假定一个404页面至少需要5秒钟才能被提供? - ficuscr
是的,5秒钟是相当激进的默认设置。如果您连接的服务器速度较慢,可以将其增加到30秒,但这会损害用户体验。 - uberweb

1
我遇到了一个非常类似的结果/问题。关键是我没有确认我的URL在ajax函数的数据部分中不包含任何相同的参数名称。
我之所以这样做,是因为我用php代码生成了一个URL。
pines_url('com_referral', 'sendhttprequest')

该句话的意思是:输出到一个带有参数的URL。其中保留了HTML格式,不做解释。
option=com_referral

并且有一个参数

action=sendhttprequest

我的一个大问题是,在 AJAX 函数的数据部分还尝试发送一个名为“action”的参数。

data: {"action": "getpoints"}

所以第二个动作是覆盖它,"getpoints"不存在,但棘手的部分是firebug显然不会告诉我带有getpoints的url,它会告诉我使用sendhttprequest的url。
希望这不会让人困惑,但我也希望这能帮助其他遇到类似问题的人!

0

在你的代码中删除 'url: '

你的代码:

$.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);
    }
});

正确的代码:

$.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);
    }
});


0

我不确定为什么,但如果您使用链接的.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);
    });

我刚试了一下,如果调用是跨站请求并且得到404,则不会执行失败方法。 - WoodyDRN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接