在AJAX中redirect_to的正确方法是什么?

时间:2013-08-07 11:22:53

标签: ruby-on-rails ruby ajax jquery

我通过jQuery AJAX向post方法发送信息。然后我想重定向到我的主页。但是redirect_to root_url在这种情况下不起作用。从ajax帖子重定向的正确方法是什么?

jQuery的:

function responseCallbackHandler(response) {
   switch (response.status) {
     case 400:
         console.log(response.error);
         break;
     case 201:
         $.ajax({ url: '#{addbank_bankaccts_path}',
         type: 'POST',
         beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
         dataType: "json",
         data: 'account_uri=' + response.data.uri
         });
         break;
     }
 }

控制器:

class BankacctsController < ApplicationController
  def addbank
      # some logic here
      redirect_to root_url, :notice => "Back account added!"
  end
end

1 个答案:

答案 0 :(得分:1)

您不能将redirect_to用于ajax请求。在堆栈溢出时遵循类似的答案,它将解决您的问题:

Is it possible to use redirect_to when it's called by remote ajax?