[英]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);
}
});
}
如果服务器返回200 OK,一切正常。 但是,如果出现问题,服务器将回答500。如何将响应主体返回给调用方法?
错误体在ajaxRequest方法上用console.log打印,但是没有返回到调用方法吗?
给定问题$.when()
处的js
是不必要的,因为$.ajax()
返回一个jQuery Promise对象。 var requestedData;
未设置为值,则在.done()
处undefined
; 将.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;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.