jQuery 后失败回调不显示代码 400 的响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36016947/
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
jQuery post fail callback does not display response for code 400
提问by Gacek
I have a following jQuery post request:
我有以下 jQuery 发布请求:
$.post("ajax.php", data).done(
function(response){
// do something when response is ok
}
).fail(
function(response){
$("#error").html(response);
}
);
In my ajax.php
file when something goes wrong I return HTTP response code 400 with a message:
在我的ajax.php
文件中出现问题时,我会返回 HTTP 响应代码 400 并显示一条消息:
header("HTTP/1.0 400 Error");
echo "Some error message";
exit();
When I check the response for faulty call in my browser I can see the status code Status Code:400 Bad Request
and a response text I passed in error message. But the jQuery's .fail
callback is not displaying my response
.
当我在浏览器中检查错误调用的响应时,我可以看到状态代码Status Code:400 Bad Request
和我在错误消息中传递的响应文本。但是 jQuery 的.fail
回调没有显示我的response
.
How can I get the access to the fail/error response text?
如何访问失败/错误响应文本?
EDIT
编辑
I tested my code and the .fail
callback is being triggered, I just cannot get the response message being displayed.
我测试了我的代码并且.fail
正在触发回调,我只是无法显示响应消息。
回答by KAD
You need to use jqXHR
:
您需要使用jqXHR
:
The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method. When the transport mechanism is something other than XMLHttpRequest (for example, a script tag for a JSONP request) the jqXHR object simulates native XHR functionality where possible.
从 jQuery 1.5 开始,$.ajax() 返回的 jQuery XMLHttpRequest (jqXHR) 对象是浏览器原生 XMLHttpRequest 对象的超集。例如,它包含 responseText 和 responseXML 属性,以及 getResponseHeader() 方法。当传输机制不是 XMLHttpRequest 时(例如,JSONP 请求的脚本标记),jqXHR 对象尽可能模拟本机 XHR 功能。
.fail
is one the available Promise methods of the jqXHR object,
.fail
是 jqXHR 对象的可用 Promise 方法之一,
jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
You can use the responseText
property to get the error message as :
您可以使用该responseText
属性来获取错误消息:
$.post("ajax.php", data).done(
function(response){
// do something when response is ok
}
).fail(
function(jqXHR, textStatus, errorThrown) {
$("#error").html(jqXHR.responseText);
}
);
Reference : http://api.jquery.com/jquery.ajax/#jqXHR
回答by Erik
I always use 500 for an ajax fail code and it usually gets picked up by .fail for me. JQ API for .post has a long list of nested links describing how they handle JqXHR object and how it interacts with JQ as a whole.
我总是使用 500 作为 ajax 失败代码,它通常会被 .fail 选中。.post 的 JQ API 有一长串嵌套链接,描述了它们如何处理 JqXHR 对象以及它如何作为一个整体与 JQ 交互。
Since you're not triggering .fail with your status, you could have your success check for your specific status code in the .success or .done function like so.
由于您没有触发 .fail 与您的状态,您可以像这样在 .success 或 .done 函数中检查您的特定状态代码的成功。
//post stuff
.success(function(response){
if(response.status == 400){
//error stuff
}
});
The advantage of performing your own checks in .done or .success is that you can define some nifty behaviors for different statuses you'd like to return from your server. There are literally dozens of different ways to handle ajax returns and define callback behaviors.
在 .done 或 .success 中执行自己的检查的优点是您可以为想要从服务器返回的不同状态定义一些漂亮的行为。实际上有几十种不同的方法来处理 ajax 返回和定义回调行为。
**Here's another SO question that has some other resolutions.
**这是另一个有其他解决方案的 SO 问题。
jQuery AJAX Error Handling (HTTP Status Codes)
**The syntax here is slightly different because this question focused on the .ajax method instead of .post, but the ideas and resolutions proposed should still work.
**这里的语法略有不同,因为这个问题关注的是 .ajax 方法而不是 .post,但提出的想法和解决方案应该仍然有效。