jQuery:执行同步 AJAX 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6685249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:14:02  来源:igfitidea点击:

jQuery: Performing synchronous AJAX requests

ajaxjquerysynchronous

提问by Industrial

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.

我过去做过一些 jQuery,但我完全坚持这一点。我知道使用同步 ajax 调用的优缺点,但这里需要它。

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;

}

回答by Dogbert

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

示例 - http://api.jquery.com/jQuery.ajax/#example-3

PLEASE NOTE:Setting async property to false is deprecatedand 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:

请注意:不推荐将 async 属性设置为 false并且正在被删除(链接)。如果你使用这个,包括 Firefox 和 Chrome 在内的许多浏览器已经开始在控制台中打印警告:

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/.

主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响。如需更多帮助,请查看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/

主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响。如需更多帮助http://xhr.spec.whatwg.org/

回答by Jake

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

您错误地使用了 ajax 函数。由于它是同步的,它将像这样内联返回数据:

var remote = $.ajax({
    type: "GET",
    url: remote_url,
    async: false
}).responseText;

回答by TheBrain

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;

回答by user3116547

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;
        }
    });
}