Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: get url in success function

Tags:

jquery

ajax

I would like to get the url from a jQuery success function, after it retreives data from the server. Unfortunately, the three parameters the success function takes don't expose the original url:

success: function(data, statusText, jqhxr)

I dumped the jqhxr variable, and couldn't find the url in there. The reason I need the url is because I'm making several calls at once through a loop, and therefore I have no idea which Ajax call is coming back. Making it an asynchronous call didn't help either.

Thank you in advance!

like image 292
StackOverflowed Avatar asked Mar 22 '12 21:03

StackOverflowed


2 Answers

this.url inside success function will work because this refers to the current context of the function, and since the success function is part of the settings object that you're passing to .ajax() it will access the url property.

See an article describing js scope and .ajax().

like image 142
Esailija Avatar answered Sep 17 '22 14:09

Esailija


you mean making it a synchronous call didn't help...?

and my suggestion would be to set a parameter in the data you're getting back to determine what to do with it, if you're looping through a bunch of AJAX calls. But really you should have to loop through more than one call if you're doing it right you should be able to send enough parameters along with the data to get it all back in one call, this reduces the number trips between client and server, server and database, then server back to client.

like image 28
Eric Hodonsky Avatar answered Sep 20 '22 14:09

Eric Hodonsky