获取与AJAX错误的服务器响应? [英] Get server response with AJAX error?
问题描述
有一个不正确的阿贾克斯行动,我用HTTP头code设置为403,并发送以下响应:
{code:403,状态:故宫,消息:你不能这样做}
不过,我不能处理我的错误,当访问这些数据?是否有可能接取从jqXHR消息的数据?
像jqXHR.message?
非常感谢您的帮助......
编辑:
错误:函数(XHR){
$(警报)HTML(xhr.responseText);
},
这将返回:
{code:403,状态:故宫,消息:你不能这样做}
但xhr.responseText.message不返回任何东西......
编辑:这code作品:
错误:函数(XHR){
VAR jsonResponse = JSON.parse(xhr.responseText);
$(警报)HTML(jsonResponse.message);
},
您应该得到jQuery的'错误'回调... http://api.jquery.com/jQuery.ajax/
错误:函数(XHR,状态,错误){
警报(xhr.responseText);
}
(顺便说一句..乌尔code?)
for an incorrect Ajax action, I set with HTTP header code to 403 and send the following response :
{"code":"403","status":"Forbidden","message":"You cannot do this"}
However, I can't access this data when handling my error... Is it possible to acess "message"data from jqXHR ?
something like jqXHR.message ?
Many thanks for your help...
EDIt :
error: function (xhr) {
$(".alert").html(xhr.responseText);
},
This returns :
{"code":"403","status":"Forbidden","message":"You cannot do this"}
But xhr.responseText.message doesn't return anything ...
EDIT : this code works :
error: function (xhr) {
var jsonResponse = JSON.parse(xhr.responseText);
$(".alert").html(jsonResponse.message);
},
You should be getting in jQuery 'error' callback... http://api.jquery.com/jQuery.ajax/
error: function(xhr, status, error) {
alert(xhr.responseText);
}
(btw.. ur code?)
这篇关于获取与AJAX错误的服务器响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!