jQuery .get 错误响应函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4062317/
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:36:12  来源:igfitidea点击:

jQuery .get error response function?

jquery

提问by Sahas Katta

Thanks to StackOverflow, I managed to get the following code working perfectly, but I have a follow up question.

感谢 StackOverflow,我设法让以下代码完美运行,但我有一个后续问题。

$.get('http://example.com/page/2/', function(data){ 
  $(data).find('#reviews .card').appendTo('#reviews');
});

This code above enabled my site to fetch a second page of articles with a Load More button in WordPress. However when the site runs out of pages/results, I'm running into a small issue. The load more button remains in place. It will keep trying to fetch data even if no pages are remaining.

上面的代码使我的网站能够通过 WordPress 中的“加载更多”按钮获取文章的第二页。但是,当网站的页面/结果用完时,我遇到了一个小问题。加载更多按钮保持原位。即使没有剩余页面,它也会继续尝试获取数据。

How can I tweak this command so that I can show a visual response if the .getrequest fails or is a (404) not found page for instance?

如何调整此命令,以便在.get请求失败或找不到 (404) 页面时显示视觉响应?

If someone could help me out with a simple example of even an alert("woops!");that would be awesome!

如果有人能用一个简单的例子来帮助我alert("woops!");,那就太棒了!

Thanks!

谢谢!

回答by lonesomeday

$.getdoes not give you the opportunity to set an error handler. You will need to use the low-level $.ajaxfunction instead:

$.get不会给您设置错误处理程序的机会。您将需要使用低级$.ajax函数:

$.ajax({
    url: 'http://example.com/page/2/',
    type: 'GET',
    success: function(data){ 
        $(data).find('#reviews .card').appendTo('#reviews');
    },
    error: function(data) {
        alert('woops!'); //or whatever
    }
});


Edit March '10

10 年 3 月编辑

Note that with the new jqXHRobject in jQuery 1.5, you can set an error handler after calling $.get:

请注意,使用jqXHRjQuery 1.5 中的新对象,您可以在调用后设置错误处理程序$.get

$.get('http://example.com/page/2/', function(data){ 
    $(data).find('#reviews .card').appendTo('#reviews');
}).fail(function() {
    alert('woops'); // or whatever
});

回答by Nick Craver

If you want a generic error you can setup all $.ajax()(which $.get()uses underneath) requests jQuery makes to display an error using $.ajaxSetup(), for example:

如果你想要一个通用错误,你可以设置所有$.ajax()$.get()在下面使用)jQuery 使用 显示错误的请求$.ajaxSetup(),例如:

$.ajaxSetup({
  error: function(xhr, status, error) {
    alert("An AJAX error occured: " + status + "\nError: " + error);
  }
});

Just run this once before making any AJAX calls (no changes to your current code, just stick this before somewhere). This sets the erroroption to defaultto the handler/function above, if you made a full $.ajax()call and specified the errorhandler then what you had would override the above.

只需在进行任何 AJAX 调用之前运行一次(不更改当前代码,只需将其粘贴在某处之前)。这将error选项设置为默认为上面的处理程序/函数,如果您进行了完整$.ajax()调用并指定了error处理程序,那么您所拥有的内容将覆盖上述内容。

回答by Hiral

You can get detail error by using responseText property.

您可以使用 responseText 属性获取详细错误。

$.ajaxSetup({
error: function(xhr, status, error) {
alert("An AJAX error occured: " + status + "\nError: " + error + "\nError detail: " + xhr.responseText);
     } 
    });

回答by Mahbub

You can chain .fail()callback for errorresponse.

您可以链接错误响应的.fail()回调。

$.get('http://example.com/page/2/', function(data){ 
   $(data).find('#reviews .card').appendTo('#reviews');
})
.fail(function() {
  //Error logic
})