如何在Rails 3中使用AJAX请求实现重定向响应? [英] How to implement redirect response with an AJAX request in Rails 3?

查看:55
本文介绍了如何在Rails 3中使用AJAX请求实现重定向响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的场景,我想请求一个页面.请求格式为AJAX.如果该请求的控制器/操作逻辑中有错误,我想重定向到错误页面.问题是重定向不是JavaScript响应类型,因此我不确定重定向是否有效.

I have a simple scenario where I want to request a page. The request format is AJAX. If there is some error in the controller/action logic for that request, I want to redirect to an error page. The issue is that a redirect is not a JavaScript response type, so I am not sure whether it will work.

如果没有错误,那么我希望通过适当的JavaScript响应类型来更新页面.

If there are no errors, then I want the page to be updated via the appropriate JavaScript response type.

在请求格式为AJAX的情况下,实现重定向响应的最佳实践是什么?

What is best practice to achieve redirect responses given that the request format is AJAX?

推荐答案

This blog post enlightened me on what I think is the right way to do this, if your ajax response is ajax; at least, in the unobtrusive javascript paradigm. In essense, the ajax call always returns a standard json package, which can be parsed for information payload or a redirect url.

这篇关于如何在Rails 3中使用AJAX请求实现重定向响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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