未设置XHR statusText

问题描述 投票:1回答:2

什么会导致XHR被覆盖?我假设这就是这里发生的事情。

我正在设置状态和代码,在此处显示,带有帮助程序类:

 if (program.Name == programName)
 {         
    ServiceHelper.SetHttpError(501, "'Program Name' Already Exists.'");
    return;       
 }

class:

public static void SetHttpError(int statusCode, string message)
{
   HttpContext.Current.Response.StatusCode = statusCode;
   HttpContext.Current.Response.StatusDescription = message;
}

处理xhr:

function CallService(method, jsonParameters, successCallback, errorCallback) 
        {
            if (errorCallback == undefined) 
            {
                errorCallback = function(xhr) {
                    if (xhr.status == 501) {
                        alert(xhr.statusText);
                    }
                    else {
                        alert("Unexpected Error");
                    }
                }
            }            

            $.ajax({
                type: "POST",
                url: method,
                data: jsonParameters,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: successCallback,
                error: errorCallback
            });
        }

一次它正在工作..现在警报显示的全部是“错误”,而不是我提供的消息。.

任何想法?

asp.net json error-handling xmlhttprequest
2个回答
1
投票

您正在使用哪个版本的jQuery?最新的文档说错误回调的签名是:

error(jqXHR, textStatus, errorThrown)

您的消息可能在textStatus参数中。

您是否尝试过使用FireBug打破错误功能并查看xhr对象的属性?


0
投票

我遇到了一个类似的问题,即未设置statusText,但仅在IIS上使用HTTPS时才设置,而在纯HTTP上也可以使用。我最终发现IIS only supports HTTP2 on TLS,并且在HTTP2中不再有状态码描述。当我使用Fiddler进行故障排除时,它可能同时在http和https中起作用,可能是因为this

在ASP.NET MVC中,您不再可以使用HttpStatusCode(code,description),因为描述将无处可去。您必须通过说明,例如放入响应主体。或暂时禁用HTTP2。

© www.soinside.com 2019 - 2024. All rights reserved.