我怎样才能重定向ASP.MVC当一个Ajax请求时? [英] How can I redirect in ASP.MVC when an Ajax request is made?

查看:130
本文介绍了我怎样才能重定向ASP.MVC当一个Ajax请求时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,例如当发生某种异常或授权失败重定向到另一个页面内的操作方法。这不是正常的请求的问题,然而,当该请求被通过Ajax制成,重定向失败。 现在有不少人在那里似乎表明,我应该发回一个 JavaScriptResult 并修改 document.location ,但我不能确定这是否是最佳的解决方案。有没有其他方法来实现这一目标?谢谢你。

I would like to redirect to another page within an action method when for instance a certain exception occurs or authorization fails. This is not a problem with normal requests, however when the request is made via Ajax, redirection fails. Many folks out there seem to suggest that I should send back a JavaScriptResult and change the document.location, but I'm not sure if this is the best solution. Are there any alternative ways to achieve this? Thanks.

P.S。我使用ASP.NET MVC 1.0。

P.S. I am using ASP.NET MVC v1.0.

推荐答案

做一个重定向通过AJAX是用JavaScript,您是否传递回JavaScriptResult与设置位置,或在新的URL脚本的唯一方法JSON和设置在回调处理程序中的位置。我觉得选择实际上取决于你将如何处理请求的结果并非如此。如果它总是要重定向,我只会给没有Ajax请求。如果正常,你会返回的HTML被放在页面上,然后JavaScriptResult可能会更好。如果你通常得到JSON和更新通过code中的页面,然后我会回送一个JSON响应包含URL,让回调处理程序设置的位置。

The only way to do a "redirect" via AJAX is with javascript, whether you pass back a JavaScriptResult with a script that sets the location or the new URL in JSON and set the location in the callback handler. I think the choice really depends on how you would handle the result of the request otherwise. If it's always going to redirect, I would simply make the request without AJAX. If normally, you'd return HTML that gets put on the page, then a JavaScriptResult may be better. If you're typically getting JSON and updating the page via code, then I'd send back a JSON response containing the URL and let the callback handler set the location.

这篇关于我怎样才能重定向ASP.MVC当一个Ajax请求时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆