如何在Rails 3中使用AJAX请求实现重定向响应?

时间:2022-10-08 22:52:01

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.

我有一个简单的场景,我想要一个页面。请求格式是AJAX。如果该请求的控制器/操作逻辑中存在一些错误,我想重定向到错误页面。问题是重定向不是JavaScript响应类型,所以我不确定它是否会起作用。

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

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

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

考虑到请求格式是AJAX,实现重定向响应的最佳做法是什么?

3 个解决方案

#1


3  

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.

如果您的ajax响应是ajax,那么这篇博文可以让我了解我认为正确的方法。至少,在不引人注目的javascript范例中。在本质上,ajax调用总是返回一个标准的json包,可以解析它的信息有效负载或重定向url。

#2


3  

You can also put this in your ApplicationController to redirect properly for AJAX requests:

您还可以将它放在ApplicationController中,以便为AJAX请求正确地重定向:

class ApplicationController < ActionController::Base

  # Allows redirecting for AJAX calls as well as normal calls
  def redirect_to(options = {}, response_status = {})
    if request.xhr?
      render(:update) {|page| page.redirect_to(options)}
    else
      super(options, response_status)
    end
  end

end

#3


1  

If you use jQuery, you can use .ajaxError()

如果你使用jQuery,你可以使用.ajaxError()

$(document).ajaxError(function(event, request, settings){
  location.href = '/error.html';
});

or assume you do a ajax post

或者假设你做了一个ajax帖子

var jqxhr = $.post("example.php", function() {
  // Do what it completed will do
})
.error(function() { 
  location.href = '/error.html';
})

#1


3  

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.

如果您的ajax响应是ajax,那么这篇博文可以让我了解我认为正确的方法。至少,在不引人注目的javascript范例中。在本质上,ajax调用总是返回一个标准的json包,可以解析它的信息有效负载或重定向url。

#2


3  

You can also put this in your ApplicationController to redirect properly for AJAX requests:

您还可以将它放在ApplicationController中,以便为AJAX请求正确地重定向:

class ApplicationController < ActionController::Base

  # Allows redirecting for AJAX calls as well as normal calls
  def redirect_to(options = {}, response_status = {})
    if request.xhr?
      render(:update) {|page| page.redirect_to(options)}
    else
      super(options, response_status)
    end
  end

end

#3


1  

If you use jQuery, you can use .ajaxError()

如果你使用jQuery,你可以使用.ajaxError()

$(document).ajaxError(function(event, request, settings){
  location.href = '/error.html';
});

or assume you do a ajax post

或者假设你做了一个ajax帖子

var jqxhr = $.post("example.php", function() {
  // Do what it completed will do
})
.error(function() { 
  location.href = '/error.html';
})