Asp.net mvc 在ajax中未接收Web API HttpResponseMessage?
第一次尝试Web API时有点困惑 我在Web Api中设置了一个基本的删除:Asp.net mvc 在ajax中未接收Web API HttpResponseMessage?,asp.net-mvc,jquery,asp.net-mvc-5,asp.net-web-api2,Asp.net Mvc,Jquery,Asp.net Mvc 5,Asp.net Web Api2,第一次尝试Web API时有点困惑 我在Web Api中设置了一个基本的删除: public HttpResponseMessage Delete(string id) { HttpResponseMessage response = new HttpResponseMessage(); response.ReasonPhrase = "User successfully deleted"; response.StatusCode = HttpStatusCode.OK;
public HttpResponseMessage Delete(string id)
{
HttpResponseMessage response = new HttpResponseMessage();
response.ReasonPhrase = "User successfully deleted";
response.StatusCode = HttpStatusCode.OK;
return response;
}
并通过jquery ajax调用:
deleteUser: function (data) {
var self = this;
$.ajax({
type: "DELETE",
url: urlPath + data.Id,
success: function (response) {
alert("Success: " + response.status + " : " + response.statusText);
},
error: function (response) {
alert("Error: " + response.status + " : " + response.statusText);
}
});
}
这很有效。。。Chrome开发者工具显示状态代码:200用户成功删除
不幸的是,来自ajaxsuccess的警报只是说“success:undefined:undefined”,当在Chrome中打开success函数时,响应变量为空
如何在ajax调用中检索状态代码/消息以显示在屏幕上
谢谢jQuery回调函数的参数是:
function(PlainObject data, String textStatus, jqXHR jqXHR)
如您所见,第一个参数是原始响应主体。如果需要HTTP状态代码和文本,请使用jqXHR
(第三个参数)数据:
而不是
response.statusText
尝试response.responseText
success: function(data, status, xhr)
{
alert("Success: " + xhr.status + " : " + xhr.statusText);
}