JQuery ajax 在 $.when 服务器 returns 500 错误时停止
JQuery ajax stoppen in $.when when server returns 500 error
我使用 JQuery 和 $.when 方法向服务器发出请求。
$.when(ajaxRequest(param)).done(function(response){
console.log(responseData);
});
我的 ajax 函数如下所示:
function ajaxRequest(param){
var requestedData;
return $.ajax({
type: 'POST',
url: myurl,
data: {
setParam:param
},
error: function(data){
console.log(data);
return(data);
}
});
}
如果服务器 return 为 200 正常,则一切正常。但是,如果出现问题,服务器会返回 500。我如何 return 调用方法的响应主体?
Errorbody 在 ajax 请求方法上用 console.log 打印,但它没有 return 到调用方法?
鉴于问题 $.when()
中的 js
不是必需的,因为 $.ajax()
returns jQuery promise 对象。 var requestedData;
未设置为值,将 undefined
设为 .done()
;使用 .then()
或 .done()
可用的 response
作为返回数据; .then()
处理成功和错误响应
function ajaxRequest(param){
return $.ajax({
type: 'POST',
url: myurl,
data: {
setParam:param
}
});
}
ajaxRequest(param)
.then(function(response){
console.log(response);
return response
}
// handle errors at second function of `.then()`
, function err(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
return errorThrown;
});
我使用 JQuery 和 $.when 方法向服务器发出请求。
$.when(ajaxRequest(param)).done(function(response){
console.log(responseData);
});
我的 ajax 函数如下所示:
function ajaxRequest(param){
var requestedData;
return $.ajax({
type: 'POST',
url: myurl,
data: {
setParam:param
},
error: function(data){
console.log(data);
return(data);
}
});
}
如果服务器 return 为 200 正常,则一切正常。但是,如果出现问题,服务器会返回 500。我如何 return 调用方法的响应主体?
Errorbody 在 ajax 请求方法上用 console.log 打印,但它没有 return 到调用方法?
鉴于问题 $.when()
中的 js
不是必需的,因为 $.ajax()
returns jQuery promise 对象。 var requestedData;
未设置为值,将 undefined
设为 .done()
;使用 .then()
或 .done()
可用的 response
作为返回数据; .then()
处理成功和错误响应
function ajaxRequest(param){
return $.ajax({
type: 'POST',
url: myurl,
data: {
setParam:param
}
});
}
ajaxRequest(param)
.then(function(response){
console.log(response);
return response
}
// handle errors at second function of `.then()`
, function err(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
return errorThrown;
});