Industrial Industrial - 7 months ago 103
Javascript Question

jQuery: Performing synchronous AJAX requests

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;

}

Answer

As you're making a synchronous request, that should be

function getRemote() {
    return $.ajax({
        type: "GET",
        url: remote_url,
        async: false
    }).responseText;
}

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). Doesn't work on latest Chrome already (2016-04-26).

Comments