Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting after Ajax post

Tags:

jquery

ajax

I want the success on ajax post to go to the home page. For some reason I keep doing it wrong. Any idea what I should do to fix this?

window.APP_ROOT_URL = "<%= root_url %>";

Ajax

$.ajax({ url: '#{addbank_bankaccts_path}',
 type: 'POST',
 beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
 dataType: "json",
 data: 'some_uri=' + response.data.uri ,
 success: function(APP_ROOT_URL) {
      window.location.assign(APP_ROOT_URL);
  }
});
like image 942
Alain Goldman Avatar asked Aug 08 '13 05:08

Alain Goldman


People also ask

Can you redirect from ajax?

Add a middleware to process response, if it is a redirect for an ajax request, change the response to a normal response with the redirect url. Then in ajaxComplete, if the response contains redirect, it must be a redirect, so change the browser's location.

How do I stop ajax from redirecting?

ajax can't have any option where you can prevent redirection. You can see that HTTP redirection is the part of HTTP protocol and not a part of XMLHttpRequest . So it's on the another level of abstraction or the network stack.

How do I redirect a page in ajax?

ajax({ type: 'POST', url: 'b. php', data: 'result='+$name, success: function() { window. location. href = "profile.


1 Answers

success: function(response){
    window.location.href = response.redirect;
}

Hope the above will help because I had the same problem

like image 67
backtrack Avatar answered Oct 05 '22 20:10

backtrack