【发布时间】:2013-10-24 15:54:36
【问题描述】:
对于不正确的 Ajax 操作,我将 HTTP 标头代码设置为 403 并发送以下响应:
{"code":"403","status":"Forbidden","message":"You cannot do this"}
但是,在处理我的错误时,我无法访问这些数据...是否可以从 jqXHR 访问“消息”数据?
类似 jqXHR.message 的东西?
非常感谢您的帮助...
编辑:
error: function (xhr) {
$(".alert").html(xhr.responseText);
},
这会返回:
{"code":"403","status":"Forbidden","message":"You cannot do this"}
但是 xhr.responseText.message 没有返回任何内容...
编辑:此代码有效:
error: function (xhr) {
var jsonResponse = JSON.parse(xhr.responseText);
$(".alert").html(jsonResponse.message);
},
【问题讨论】:
-
stackoverflow.com/q/1637019/139010 虽然注意 cmets 关于使用
JSON.parse()而不是eval。 -
应该是jsonResponse["message"],不是吗?