RedirectToRoute(QUOT;默认")和重定向(RETURNURL)正在返回'200'而不是'302' [英] RedirectToRoute("Default") and Redirect(returnUrl) are returning '200' instead of '302'
问题描述
我尝试使用下面的code到登录的用户:
I'm trying to use the following code to log users in:
$("form").submit(function(event)
{
$.ajax({
type: "POST",
url: $(this).attr("action"),
data: $(this).serialize(),
complete: function(XMLHttpRequest, textStatus)
{
if (XMLHttpRequest.status === 302)
{
// if they've successfully logged in, I redirect to their page
window.location = XMLHttpRequest.getResponseHeader("Location");
}
}
});
return false;
});
在服务器上的以下code处理后:
with the following code on the server to handle the post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(MyUser user)
{
if (ValidateLogOn(user.UserName, user.Password))
{
if (Request.QueryString["ReturnUrl"] != null)
{
return Redirect(Request.QueryString["ReturnUrl"]);
}
else
{
return RedirectToRoute("Default");
}
}
}
我想这两个重定向操作的结果被认为发行302S但XMLHtt prequest.status值始终为200。有什么我做错了吗?
I thought both 'Redirect' Action Results were supposed to issue 302s but the XMLHttpRequest.status value is always 200. Is there something I'm doing wrong?
编辑:这是什么最终制定
$("form").submit(function(event)
{
$.ajax({
type: "POST",
url: $(this).attr("action"),
data: $(this).serialize(),
complete: function(xhr, textStatus)
{
var dataType = xhr.getResponseHeader("Content-Type");
if (dataType.indexOf("json") > -1)
{
window.location = JSON.parse(xhr.responseText).returnUrl;
}
if (dataType.indexOf("html") > -1)
{
$('#main').html(xhr.responseText);
}
}
});
return false;
});
在服务器上的以下内容:
with the following on the server:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(MyUser user, string returnUrl)
{
if (!ValidateLogOn(user.UserName, user.Password))
{
return PartialView();
}
else
{
user = (MyUser)Session["CurrentUser"];
if (user.IsPublic)
{
return Json(new { returnUrl = Url.RouteUrl("PublicRoute") });
}
else
{
if (!string.IsNullOrEmpty(returnUrl))
{
return Json(new { returnUrl = returnUrl.ToLower() });
}
else
{
return Json(new { returnUrl = Url.RouteUrl("AdminRoute") });
}
}
}
}
感谢
戴夫
推荐答案
您的浏览器将自动跟随重定向并请求重定向页面,然后返回 200
。运行一个代理(如小提琴手或查尔斯)要明白我的意思。
Your browser is automatically following the redirect and requesting the redirect page, which is then returning 200
. Run a proxy (like Fiddler or Charles) to see what I mean.
我建议返回 200
状态,但使用JSON对象来指定要重定向用户。
I'd recommend returning a 200
status, but use a JSON object to specify that you want to redirect the user.
此外,<一个href=\"http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call/1534662#1534662\">this回答建议在这种情况下使用状态 278
(显然未使用状态code,这听起来危险对我来说)。
Additionally, this answer recommends using status 278
(apparently an unused status code, which sounds dangerous to me) in this scenario.
这篇关于RedirectToRoute(QUOT;默认&QUOT;)和重定向(RETURNURL)正在返回'200'而不是'302'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!