我正在使用$.post()Ajax调用servlet,然后使用生成的HTML片段替换div用户当前页面中的元素.但是,如果会话超时,服务器会发送重定向指令以将用户发送到登录页面.在这种情况下,jQuery正在用div登录页面的内容替换元素,迫使用户的目光确实见证了罕见的场景.
如何使用jQuery 1.2.6从Ajax调用管理重定向指令?
我的第一篇文章......
当我使用RedirectToAction时,浏览器中的url不会更改.我怎样才能做到这一点?
使用Web表单10多年后,我正在切换到ASP.NET MVC 3.0(也使用jQuery Mobile).我大约需要8个星期的时间,经过几本书并且谷歌搜索答案之后,我才会干嘛.
我在Global.asax中定义了一个路由:
routes.MapRoute(
"Routes",
"{controller}/{action}/{id}",
new { controller = "Shopping", action = "Index", id = UrlParameter.Optional }
Run Code Online (Sandbox Code Playgroud)
我有一个包含以下操作的ShoppingController:
public ActionResult Cart() {...}
public ActionResult Products(string externalId) {...}
[HttpPost]
public ActionResult Products(List<ProductModel> productModels)
{
// do stuff
return RedirectToAction("Cart");
}
Run Code Online (Sandbox Code Playgroud)
当我进行get和post(带有RedirectToAction的帖子)时的url总是:
/Shopping/Products?ExternalId=GenAdmin
Run Code Online (Sandbox Code Playgroud)
在post和RedirectToAction之后,我希望浏览器中的url更改为:
/Shopping/Cart
Run Code Online (Sandbox Code Playgroud)
我尝试过Redirect和RedirectToRoute,但得到了相同的结果.
任何帮助将不胜感激.
[更新] 我发现jQuery Mobile AJAX帖子是这里的罪魁祸首.如果我关闭jQuery Mobile的AJAX就可以了.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
// do not handle links via ajax by default
$(document).bind("mobileinit", function () { $.mobile.ajaxEnabled = false; });
</script>
<link rel="stylesheet" …Run Code Online (Sandbox Code Playgroud) 视图:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<script type="text/javascript">
$(document).ready(function() {
$(document).ready(function() {
$("#example").submit(function() {
var id = $("#id").val();
var prezime = $("#prezime").val();
$.post("/jQueryPost/PostingData", { id: id, prezime: prezime }, function(res) {
if (res.redirect) {
window.location.href = res.redirect;
return;
}
}, "json");
return false;
});
});
</script>
<form id="example" method = "post">
Id:<input id="id" type="text" />
Prezime:<input id="prezime" type="text" />
<p>
<input type="submit" value="Post Data" />
</p>
</form>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
控制器动作:
[HttpPost]
public ActionResult PostingData(int id, string prezime)
{
//some code
return …Run Code Online (Sandbox Code Playgroud)