I've done some jQuery in the past, but I am completely stuck on this. I know about the pros and cons of using synchronous ajax calls, but here it will be required.
The remote page is loaded (controlled with firebug), but no return is shown.
What should I do different to make my function to return properly?
function getRemote() {
var remote;
$.ajax({
type: "GET",
url: remote_url,
async: false,
success : function(data) {
remote = data;
}
});
return remote;
}
how remote is that url ? is it from the same domain ? the code looks okay
try this
As you're making a synchronous request, that should be
Example - http://api.jquery.com/jQuery.ajax/#example-3
PLEASE NOTE: Setting async property to false is deprecated and in the process of being removed (link). Many browsers including Firefox and Chrome have already started to print a warning in the console if you use this:
Chrome:
Firefox:
I really hate the "I don't use it or agree with it so you shouldn't use it either" attitude Tom has above. Almost as bad as considered harmful essays. Which reminds me of people arguing over single vs double quotes based on their IDE preference when both actually fit the standards.
I had an ajax problem which was only solved by adding
async: false,
to it. Using browser hangups as an excuse to argue for the deprecation (and removal) of this feature is silly. How do you know how long someone's synchronous ajax request would take? Not everyone loads or transfers huge amounts of data with these calls.My problem was a tracking system which most browsers weren't reporting the time users left the page (
onbeforeunload
), but once the ajax request was made to be synchronous, it worked for everything except Safari on iOS and other extremely outdated end of life web browsers. The amount of data I was sending was extremely insignificant, only two integers, and it was a request to the local server. The hangup for my request isn't notable at all, you'd need monitoring tools to take notice. My alternative to synchronous ajax calls would be continuous ajax calls through asetInterval
function, which massively increases server load and bandwidth. You can't make the argument that an ajax call every N seconds is better than one when someone exits the page. You just can't.The deprecation of synchronous requests is a punishment set on all web developers because some of them were using it irresponsibly.
You're using the ajax function incorrectly. Since it's synchronous it'll return the data inline like so: