jQuery AJAX see redirect as status 200 not 302?
I am using jQuery and the jQuery.form plugin to submit my form (also using ASP.Net MVC).
Problem is the user is in a section of the site that uses forms authentication and if their auth cookie expires during their time on the page instead of getting back a status of 302, which would be the redirect to the login page, I still get 200?
In FireBug I see the 302 Found and then my login page is served next as a 200 which is the status code sent back to my Ajax call. How do I detect that they have been logged out if I never see the 302 sent back to the jQuery form plugin?
I really like this solution. By changing the 302 response on ajax requests to a 401 it allows you to setup your ajax on the client side to monitor any ajax request looking for a 401 and if it finds one to redirect to the login page. Very simple and effective.
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;
}
}
Client Side Code:
$(function () {
$.ajaxSetup({
statusCode: {
401: function () {
location.href = '/Logon.aspx?ReturnUrl=' + location.pathname;
}
}
});
});
try with cache: false cache option in jquery ajax:
$.ajax({
url: "test.html",
cache: false,
success: function(html){
$("#results").append(html);
}
});
---EDIT Try with this in C# code :
protected void Page_Load(object sender, System.EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
...
}
A similar problem has been encountered before. Is the solution given in this question helpful?
链接地址: http://www.djcxy.com/p/71882.html上一篇: 重定向不起作用