15

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);
  }
});
Alain Goldman
  • 2,896
  • 5
  • 43
  • 75
  • are you sure the ajax is returning with a success? – Drew Dahlman Aug 08 '13 at 05:39
  • `window.location.href = APP_ROOT_URL` – abc123 Aug 08 '13 at 05:39
  • 1
    Do you see any messages in the browser's console? – Uooo Aug 08 '13 at 05:39
  • 1
    @abc123 has the right answer as well - but first i would confirm that you are actually getting a successful return. check the console and network. – Drew Dahlman Aug 08 '13 at 05:40
  • i get this from firebug post response window.location.href = http://localhost:3000/ – Alain Goldman Aug 08 '13 at 05:40
  • @AlainGoldman, in firebug console type APP_ROOT_URL to make sure its set. In your success handler, put an alert. Add a fail handler, also with an alert. What is the exact response from the server? – Brigand Aug 08 '13 at 05:42
  • APP_ROOT_URL works in the console but your right i dont think i'm getting a success because i just put an alert in there. – Alain Goldman Aug 08 '13 at 05:44
  • @AlainGoldman, do you have a live page we can see? Are there any errors in the console? Is `response` defined before your AJAX function is called? Do you mean to override APP_ROOT_URL in your success handler? – Brigand Aug 08 '13 at 05:47
  • no live page cause it's in a rails app but there are no errors in the console. response is defined before the AJAX function is called. No in the success i just want to make it to the root_url – Alain Goldman Aug 08 '13 at 05:49

3 Answers3

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

Hope the above will help because I had the same problem

jbrahy
  • 4,228
  • 1
  • 42
  • 54
backtrack
  • 7,996
  • 5
  • 52
  • 99
  • 2
    (-1) because `assign` is valid, and there was no indication of the JSON format (e.g. the redirect property). – Brigand Aug 08 '13 at 05:43
13

You can return the JSON from server with redirect status and redirect URL.

{"redirect":true,"redirect_url":"https://example.com/go/to/somewhere.html"}

And in your jQuery ajax handler

success: function (res) {
    // check redirect
    if (res.redirect) {
        window.location.href = res.redirect_url;
    }
}

Note you must set dataType: 'json' in ajax config. Hope this is helpful.

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
5

Not sure why, but window.location.href did not work for me. I ended up using window.location.replace instead, which actually worked.

$('#checkout').click(function (e) {
    e.preventDefault();
    $.ajax('/post/url', {
        type: 'post',
        dataType: 'json'
    })
    .done(function (data) {
        if (data.cartCount === 0) {
            alert('There are no items in cart to checkout');
        }
        else {
            window.location.replace('/Checkout/AddressAndPayment');
        }
    });
});
kimbaudi
  • 13,655
  • 9
  • 62
  • 74