【发布时间】:2015-01-17 17:19:38
【问题描述】:
我需要根据 JSON 响应中出现的字符串显示警报。 这是我的代码:
$.ajax({
method: 'POST',
url: dataString2,
data: dataString,
headers: {
"Authorization": "Basic " + btoa('' + Username + '' + ":" + '' + Password + '')
},
success: function(jqXHR) {
if (jqXHR.responseJSON('ok')) {
alert('First Real Server Configured');
}
},
statusCode: {
406 : function() {
alert('There is an unexpected string in your data.\nFix the error and try again.');
},
401 : function() {
alert('Wrong username or password.');
}
},
});
});
这是 JSON 响应:
{
"status":"ok"
}
HTTP 响应码为“200 OK”,服务器响应 Content-Type:application/json
当使用上面的代码时,我得到: Uncaught TypeError: undefined is not a function
有什么想法吗?
【问题讨论】:
-
好吧,究竟什么是“不是函数”?哪条线?
-
代码的哪一部分未定义?
-
jqXHR将是响应,不会有responseJSON属性。要访问jqXHR对象,请在success处包含(data, textStatus, jqXHR),然后在jqXHR.responseJSON处访问responseJSON- 不包含("ok") -
@Juhana,如下: if (jqXHR.responseJSON('ok')) {
-
@Guest271314 但只有当响应数据中出现字符串“ok”时,我才需要显示警报。
标签: javascript jquery ajax json