One JS file is making an ajax call. Within the success of this ajax call another AJAX call is made. The second call checks whether the email has already been registered. If it has been then the second AJAX call recieves no returned data as shown in firebug and Chrome console in the case. But the same code works just fine in localhost while the mentioned problem takes place ONLY in online server.

The hosted page is at http://twmobilefitness.com/signup/. The problem takes place when you click on the 'Register Now' link at the end. You have to register twice with the same email address if you want to get the problem happen.

2 Answers
2

What you're experiencing (AJAX works locally, but not on the server) there is a delay problem. Locally everything works that fast, that you can't see your problem. In short, this is your problem:

AJAX callback (A) executes > AJAX Callback (B) doesn't know that it has to wait for (A) > You can't see the problem in your local install as (A) is finished too fast.

So you need to find a way how to tell your Callback (B) that it has to wait for (A). Here's how:

Register Scripts and move data from PHP to JS

Register and enqueue and localize your data the right way: Wrap it in a function or method and hook it to wp_enqueue_scripts (public/themes), login_enqueue_scripts (password/login/register) or admin_enqueue_scripts. The use wp_localize_script() to move data from PHP to JS and make it accessible there.

Note: For better examples of the wrapper around the callback, take a look at commonjs or AMD and their difference.

Waiting for other AJAX responses

The interesting - and most powerful part of the whole jQuery (and other libararies) AJAX handling - question is how to wait until A is finished to then start B and its processing. The answer is "deferred" loading and "promises".

I'll add a quick example. You should maybe think about building and object and separating stuff by appending it via this. to the object, but for an example the following should be enough:

Example (A) This basically like I do it. You'll have to fill the bits your own.

@IstiaqueAhmed Above description isn't meant to be some plugin. Just the base to write your own plugin (or, in case you don't care about separation of concerns, your own theme).
– kaiserOct 14 '13 at 14:38

1

Thanks, this was a great help in a recent project. Already upvoted long time ago ;-) Also just wanted to mention that the success, error, complete callbacks are since deprecated for the promise callbacks mentioned in the answer.
– birgireAug 14 '15 at 14:52

@birgire Please edit the answer. I looked it up and only the first part is now deprecated. Thanks in advance and thanks for the kind words :)
– kaiserAug 15 '15 at 11:05