jQuery: Performing synchronous AJAX requests

2018-12-31 07:17发布

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;

}

5条回答
有味是清欢
2楼-- · 2018-12-31 07:59

how remote is that url ? is it from the same domain ? the code looks okay

try this

$.ajaxSetup({async:false});
$.get(remote_url, function(data) { remote = data; });
// or
remote = $.get(remote_url).responseText;
查看更多
若你有天会懂
3楼-- · 2018-12-31 08:07
function getRemote() {
    return $.ajax({
        type: "GET",
        url: remote_url,
        async: false,
        success: function (result) {
            /* if result is a JSon object */
            if (result.valid)
                return true;
            else
                return false;
        }
    });
}
查看更多
爱死公子算了
4楼-- · 2018-12-31 08:15

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). Many browsers including Firefox and Chrome have already started to print a warning in the console if you use this:

Chrome:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

Firefox:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user’s experience. For more help http://xhr.spec.whatwg.org/

查看更多
后来的你喜欢了谁
5楼-- · 2018-12-31 08:16

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 a setInterval 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.

查看更多
皆成旧梦
6楼-- · 2018-12-31 08:18

You're using the ajax function incorrectly. Since it's synchronous it'll return the data inline like so:

var remote = $.ajax({
    type: "GET",
    url: remote_url,
    async: false
}).responseText;
查看更多
登录 后发表回答