Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在ajax中未接收Web API HttpResponseMessage?_Asp.net Mvc_Jquery_Asp.net Mvc 5_Asp.net Web Api2 - Fatal编程技术网

Asp.net mvc 在ajax中未接收Web API HttpResponseMessage?

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;

第一次尝试Web API时有点困惑

我在Web Api中设置了一个基本的删除:

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);
         }