javascript - Synchronous wait for $.ajax call -


i need hyperlink execute ajax call, , when has completed, standard action hyperlink.

<a href="afterwards.html" target="_blank" onclick="return callfirst();">link</a> 

the javascript function calls $.ajax(), waits success or failure, returns true.

function callfirst() {     $deferred = $.ajax({                     type: "post",                     url: url,                     data: data                 });      // **todo** wait until ajax call has responded.      // return true, makes <a> tag it's standard action     return true; } 

the code must wait $.ajax succeed, return true callfirst().

$deferred.when() terminates immediately. how can made wait?

just set async property false

$deferred = $.ajax({                 type: "post",                 url: url,                 data: data,                 async: false             }); 

but better idea use callbacks.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -