【发布时间】:2018-02-02 22:09:29
【问题描述】:
这是我的 AJAX 代码:
jQuery.ajax({url: url,
method: 'GET',
data: getParams,
/*success: function (json, textStatus, jqXHR) {
if(jQuery.active <= 1){
waitDialog.removeWaitDialog();
}
createProcessButtonEvent();
ajaxError = false;
returnFunction(jqXHR.responseJSON);
},*/
complete: function(jqXHR, textStatus, errorThrown) {
if(jQuery.active <= 1){
waitDialog.removeWaitDialog();
}
createProcessButtonEvent();
var response;
if (typeof jqXHR.responseJSON === 'object') {
response = jqXHR.responseJSON;
} else {
response = jqXHR.responseText;
}
if(response.errors.length == 1){ // jsonResults.message != ''
if(!warningMessage){ //hard coded for ad designer
jQuery('#alertMessageText').text(response.errors[0].message);
jQuery('#alertMessage').show();
jQuery('#waitDialog').jqmHide();
ajaxError = true;
return;
}
warningMessage.displayMessage(response.errors[0].message);
jQuery('.popup').modal('hide');
jQuery('.popup').not('.persist').remove();
}
if (response.errors.length > 1){
for(var n = 0; n < response.errors.length; n++){
if (response.errors[n].id == 1){ // 2005
window.location.href = '/login?destination=' + encodeURIComponent(window.location.pathname + window.location.search);
}
if (response.errors[n].id == 9500){
statusMessage.displayMessage(response.errors[n].message);
}
}
ajaxError = true;
//if(errorFunction){
// errorFunction(jsonResults);
//}
waitDialog.removeWaitDialog();
}
if (!ajaxError) {
returnFunction(jqXHR.responseJSON);
}
},
dataType: 'json',
contentType: 'application/json'
});
我告诉它转到http://127.0.0.1/api/parameter,其中parameter 是无效资源。我的 API 在新标签页中返回:
{"errors":[{"id":3,"message":"GET route not defined for /api/parameter"}],data:{}}
我让它返回 500 状态代码,因为访问无效资源是一个错误。当我调用实际的 AJAX 时,我得到 jqXHR.responseJSON 是 null,jqXHR.responseText 是 ''。
我已尝试同时使用 success: 和 error: 块,并尝试了 complete:,因为在调用错误块后,我的 API 似乎正在解析调用,正如您在 cmets 中看到的那样。
所以我得到了TypeError: response is null,因为我的响应对象永远不会被填充,最奇怪的是我对parameter?parameters=here 的调用永远无法在网络中进行检查
【问题讨论】:
-
也许您已经从网络选项卡中过滤掉了 XHR - 您使用的是哪个浏览器 - 但是,阅读您问题的其余部分表明问题的标题非常具有误导性。
-
是
url同源还是跨源 -
@JaromandaX,你读懂了我的想法!我在浏览器中禁用了 XHR 过滤,按状态代码排序,然后查找我的呼叫。只有 200 和 304,没有 500。
url是始终指向我的本地主机的相对 URL。感谢阅读。 -
我将状态码更改为 501,而不是想知道为什么状态码 500 不起作用。知道何时折叠它们,而不是总是想知道为什么。
标签: javascript jquery ajax jqxhr