AJAX调用后状态码错误

时间:2013-12-16 16:02:49

标签: c# jquery asp.net ajax

我有ASP.NET Web Forms Web App项目。我在应用程序中使用Ajax调用时遇到了一些问题。

我的请求的一般工作流程是:

1)我使用jQuery在我的.ASPX页面上进行简单的AJAX调用,如下所示:

$.ajax({
        url: "/handler.ashx",
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
           //proccessing data
         },
         error: function (request, status, error) {                    
         }
        });

2)请求将在asp.net ashx处理程序中处理。如果出现问题,例如用户未经授权,将执行以下代码:

var membershipUser = Membership.GetUser();
if (membershipUser == null)
    {
      context.Response.StatusCode = 401;
      context.Response.StatusDescription = "Not Authorized";
      return;
    }

但是当我对响应标题进行推理时,它会在回复正文中使用我的唱歌页面的html返回 HTTP / 1.1 200 OK 。 我想获取服务器端状态代码,然后通过分析状态代码在客户端进行适当的重定向。像这样:

 $.ajax({
        url: getVersionUrl,
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
             //proccessing data
        },
        error: function (request, status, error) {
            if (request.status == 401) {
                window.location.href = "/sing-in.aspx";
            } else if (request.status == 500) {
                window.location.href = "/server-error.aspx";
            }
        }
    });

我有我想要处理的所有类型错误的问题 - 401,403和500.我不想从响应主体解析html来确定服务器端发生的错误类型。 我敢肯定我误解了一些非常基本和一般的东西。请帮帮我。

0 个答案:

没有答案
相关问题