【问题标题】:jQuery Ajax 404 HandlingjQuery Ajax 404 处理
【发布时间】:2010-11-25 22:30:58
【问题描述】:

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

即。

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

我再次知道 1000% 我在 firebug 中收到 404 Not Found 它永远不会触发错误。

我错过了什么吗?

【问题讨论】:

  • 我测试了完全相同的代码并正确收到了 3 个错误警报(第一个是 404)..
  • 您确定您确实收到了带有状态码 404 的响应,而不仅仅是带有状态码 200 的常规响应以及包含说明它是 404 的文本的页面吗?
  • @cambraca 这里也一样。使用 Firefox 3.6.12 和 Firebug 测试。
  • 下次使用console.log()进行调试。它节省了 javascript 锁定并且不那么突兀。它出现在您的 javascript 控制台中。
  • 现在我已经输入了我实际尝试的 URL。请立即尝试

标签: jquery


【解决方案1】:

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

【讨论】:

  • 那么问题中没有提到的非jsonp请求呢?
  • 注意,当你指定超时时间(以毫秒为单位)时,会调用错误。
  • 尽管 $.getJSON 用 .error() 方法捕获了 404 的问题
【解决方案2】:

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

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

【讨论】:

  • 不知道为什么这没有更多的赞成票,完美的作品请参阅:api.jquery.com/ajaxError
  • 谢谢@systematical - 我不确定,对我来说很好用,我在我所有网站的样板中都有它。
  • 这对我没有帮助的原因是因为在 JSONP 的情况下它仍然没有被调用。来自api.jquery.com/ajaxError:“注意:跨域脚本和跨域 JSONP 请求不调用此处理程序。”
【解决方案3】:

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


我的原因是,我用 php 代码生成了 URL 以获取 url

pines_url('com_referral', 'sendhttprequest')

输出到带有参数的url

option=com_referral

还有一个参数

action=sendhttprequest

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

data: {"action": "getpoints"}

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

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

    • 这假设 404 至少需要 5 秒才能提供服务?
    • 是的,5 秒是一个相当激进的默认值。如果您连接到速度较慢的服务器,则最多可能需要 30 秒,但会损害用户体验。
    【解决方案5】:

    我不知道具体原因,但是如果你使用链接.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,fail方法将不会执行。
    【解决方案6】:

    删除代码中的“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);
        }
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-20
      • 2011-09-20
      • 2012-02-25
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      相关资源
      最近更新 更多