如何检测 JQuery $.get 失败?求简单的代码示例

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

How to detect JQuery $.get failure? Seeking simple code example

jquery

提问by Mawg says reinstate Monica

I'm a JQuery n00b. I'm trying to code a very simple using $.get(). The official documentationsays

我是一个 JQuery n00b。我正在尝试使用 $.get() 编写一个非常简单的代码。在官方文件说:

If a request with jQuery.get() returns an error code, it will fail silently 
unless the script has also called the global .ajaxError()  method or. 

As of jQuery 1.5, the .error() method of the jqXHR object 
returned by jQuery.get() is also available for error handling.

So, if all goes well, my callback function for success will be called. However, if the request fails, I would like to get the HTTP code :404, 502, etc and formulate a meaningful error message for the user.

所以,如果一切顺利,我的成功回调函数将被调用。但是,如果请求失败,我想获取 HTTP 代码:404、502 等,并为用户制定有意义的错误消息。

However, since this is an asynchronous call I can imagine that I might have several outstanding. How would .ajaxError() know which request it corresponds to? Maybe it would be better to use the .error() method of the jqXHR object returned by jQuery.get()?

但是,由于这是一个异步调用,我可以想象我可能有几个未完成的。.ajaxError() 如何知道它对应的是哪个请求?也许使用 jQuery.get() 返回的 jqXHR 对象的 .error() 方法会更好?

Can someone please provide an extremely simple code example? Perhaps the success routine calls Alert("Page found") and the failure routine checks for 404 and does an Alert("Page not found")

有人可以提供一个非常简单的代码示例吗?也许成功例程调用 Alert("Page found") 并且失败例程检查 404 并执行 Alert("Page not found")



Update: the following page is extremely helpful ... http://api.jquery.com/jQuery.get/

更新:以下页面非常有用...... http://api.jquery.com/jQuery.get/

回答by Dave Ward

You're right that you can use jQuery 1.5's new jqXHR to assign error handlers to $.get()requests. This is how you can do it:

您是对的,您可以使用 jQuery 1.5 的新 jqXHR 为$.get()请求分配错误处理程序。你可以这样做:

var request = $.get('/path/to/resource.ext');

request.success(function(result) {
  console.log(result);
});

request.error(function(jqXHR, textStatus, errorThrown) {
  if (textStatus == 'timeout')
    console.log('The server is not responding');

  if (textStatus == 'error')
    console.log(errorThrown);

  // Etc
});

You can also chain handlers directly onto the call:

您还可以将处理程序直接链接到调用上:

$.get('/path/to/resource.ext')
     .success(function(result) { })
     .error(function(jqXHR, textStatus, errorThrown) { });

I prefer the former to keep the code less tangled, but both are equivalent.

我更喜欢前者让代码不那么混乱,但两者是等价的。

回答by Pekka

.get()is just a synonym for .ajax()with a number of options pre-set. Use ajax()to get the full range of options, including the errorcallback.

.get()只是.ajax()预置多个选项的同义词。使用ajax()得到全方位的选项,包括error回调。

$.ajax({
type: "GET",
url: "test.htm",
error: function(xhr, statusText) { alert("Error: "+statusText); },
success: function(msg){ alert( "Success: " + msg ); }
}
);

回答by stallingOne

As of jQuery 3.0 .success()and .error()are depreciated.
You can use .done()and .fail()instead

从 jQuery 3.0 开始.success().error()已折旧。
您可以使用.done()and.fail()代替

$.get( url )
    .done(function( data, textStatus, jqXHR ) {
        console.log(data);
    })
    .fail(function( jqXHR, textStatus, errorThrown ) {
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown );
    });

Source: https://api.jquery.com/jQuery.ajax/

来源:https: //api.jquery.com/jQuery.ajax/