jQuery AJAX看到重定向为状态200而不是302?

Sle*_*lee 10 asp.net-mvc jquery http-status-code-302

我正在使用jQuery和jQuery.form插件来提交我的表单(也使用ASP.Net MVC).

问题是用户在网站的一部分中使用表单身份验证,如果他们的身份验证cookie在页面上的时间到期而不是返回状态302,这将是重定向到登录页面,我仍然得到200 ?

在FireBug中,我看到302 Found,然后我的登录页面接下来是200,这是发送回我的A​​jax调用的状态代码.如果我从未看到302发送回jQuery表单插件,如何检测到它们已被注销?

Kyl*_*ers 9

我非常喜欢这个解决方案.通过将ajax请求的302响应更改为401,它允许您在客户端设置ajax以监视查找401的任何ajax请求,以及是否找到一个重定向到登录页面.非常简单有效.

Global.asax中:

protected void Application_EndRequest()
{
    if (Context.Response.StatusCode == 302 &&
        Context.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
    {
        Context.Response.Clear();
        Context.Response.StatusCode = 401;
    }
}
Run Code Online (Sandbox Code Playgroud)

客户端代码:

 $(function () {
      $.ajaxSetup({
        statusCode: {
          401: function () {
            location.href = '/Logon.aspx?ReturnUrl=' + location.pathname;
          }
        }
      });
    });
Run Code Online (Sandbox Code Playgroud)


and*_*lzo 1

尝试使用jquery ajax 中的 cache: false 缓存选项

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});
Run Code Online (Sandbox Code Playgroud)

---编辑尝试使用 C# 代码:

protected void Page_Load(object sender, System.EventArgs e)
{
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   ...
}
Run Code Online (Sandbox Code Playgroud)