0

I have a standard ajax call from jquery as below.

I would like to check if the json array returned is requesting a new page, and if yes, go there and do not execute the .always callback.

Is there a simple way to do this?

var jqxhr = $.ajax({ url: server_URL + '/index.php', type: 'post', dataType: "json", data: obj }).done(function (data) { if (data['go_location']) { window.location.href = data['go_location']; } else { //other stuff } }).fail(function () { alert("ajax error"); }).always(function () { //other code }); 
12
  • What are you performing in always? Commented Dec 17, 2013 at 8:25
  • Why don't you break and return when success Commented Dec 17, 2013 at 8:28
  • Are you saying that the page redirects your, i.e. 30X HTTP code? or that the JSON has a go_location variable? Commented Dec 17, 2013 at 8:29
  • JSON has a go_location variable such as browse.html i.e. array['go_location']='browse.html' Commented Dec 17, 2013 at 8:30
  • @Dipak not sure what you mean. that might be an answer? Commented Dec 17, 2013 at 8:31

2 Answers 2

2

As name says 'always', .always() is executed in both the case either success or failure. You don't wanna execute .always() on success state but failure state, which is not possible. You are calling three methods namely .done(), .fail(), and .always() on ajax response. All the method are triggered as soon as ajax is complete [.done() and .alway() on success otherwise .fail() and .always()]. We know that jquery/js is asynchronous so, even return statement in any of other method can not stop execution of .always(). In your case only option is don't use .always() method. Either of the state success or failure, either .done() or .fail() is executed. So, you can move your .always() method statements to these methods if needed.

 $.ajax({ url: server_URL + '/index.php', type: 'post', dataType: "json", data: obj }).done(function (data) { //Do something if ajax succeeds if (data['go_location']) { window.location.href = data['go_location']; } else { //other stuff } }).fail(function () { //Do something if ajax fails }) 
Sign up to request clarification or add additional context in comments.

2 Comments

are you saying .done will always run before .always (assuming success of ajax request)?
Both are triggered in the order they are chained. As I mentioned Jquery is Asynchronous and .always() will not wait for .done()'s termination.
0

I don't really understand why you have an .always function but I think you could try this:

$.ajax({ url: server_URL + '/index.php', type: 'post', dataType: "json", data: obj, success: function(data) { if (data['go_location']) { window.location.href = data['go_location']; } else { //other stuff } }, error: function(data) { alert("ajax error"); } } 

3 Comments

is success the same as .done? very unclear from the documentation what the difference is between all these handlers .done .always
I'm not really familiar with .done and .always but .success gets executed when the server sends a response back to the AJAX call.
i think .success is deprecated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.