jQuery 是否可以将 async:false 设置为 $.getJSON 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2765411/
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 14:13:27  来源:igfitidea点击:

Is it possible to set async:false to $.getJSON call

jqueryasynchronousgetjson

提问by ACP

Is it possible to set async: falsewhen calling $.getJSON()so that the call blocks rather than being asynchronous?

是否可以async: false在调用时进行设置,$.getJSON()以便调用阻塞而不是异步调用?

回答by Nick Craver

You need to make the call using $.ajax()to it synchronously, like this:

您需要$.ajax()同步使用它进行调用,如下所示:

$.ajax({
  url: myUrl,
  dataType: 'json',
  async: false,
  data: myData,
  success: function(data) {
    //stuff
    //...
  }
});

This would match currently using $.getJSON()like this:

这将匹配当前使用$.getJSON()如下:

$.getJSON(myUrl, myData, function(data) { 
  //stuff
  //...
});

回答by velja

Both answers are wrong. You can. You need to call

两个答案都是错误的。你可以。你需要打电话

$.ajaxSetup({
async: false
});

before your json ajax call. And you can set it to true after call retuns ( if there are other usages of ajax on page if you want them async )

在您的 json ajax 调用之前。您可以在调用 retuns 后将其设置为 true(如果您希望它们异步,如果页面上还有其他 ajax 用法)

回答by webdev

I think you both are right. The later answer works fine but its like setting a global option so you have to do the following:

我觉得你们俩都对。后面的答案工作正常,但就像设置全局选项一样,因此您必须执行以下操作:

    $.ajaxSetup({
        async: false
    });

    //ajax call here

    $.ajaxSetup({
        async: true
    });

回答by ronrun

In my case, Jay D is right. I have to add this before the call.

就我而言,Jay D 是对的。我必须在通话前添加这个。

$.ajaxSetup({
    async: false
});

In my previous code, I have this:

在我之前的代码中,我有这个:

var jsonData= (function() {
    var result;
    $.ajax({
        type:'GET',
        url:'data.txt',
        dataType:'json',
        async:false,
        success:function(data){
            result = data;
        }
    });
    return result;
})();
alert(JSON.stringify(jsonData));

It works find. Then I change to

它可以找到。然后我改成

var jsonData= (function() {
    var result;
    $.getJSON('data.txt', {}, function(data){
      result = data;
    });
    return result;
})();
alert(JSON.stringify(jsonData));

The alert is undefined.

警报未定义。

If I add those three lines, the alert shows the data again.

如果我添加那三行,警报会再次显示数据。

$.ajaxSetup({
    async: false
});
var jsonData= (function() {
    var result;
    $.getJSON('data.txt', {}, function(data){
      result = data;
    });
    return result;
})();
alert(JSON.stringify(jsonData));

回答by k06a

If you just need to awaitto avoid nesting code:

如果您只需要await避免嵌套代码:

let json;
await new Promise(done => $.getJSON('https://***', async function (data) {
    json = data;
    done();
}));

回答by Daff

I don't think you can set that option there. You will have to use jQuery.ajax()with the appropriate parameters (basically getJSON just wraps that call into an easier API, as well).

我认为您不能在那里设置该选项。您将不得不使用带有适当参数的jQuery.ajax()(基本上 getJSON 也只是将该调用包装到更简单的 API 中)。

回答by Stonedecroze

Roll your own e.g.

滚动你自己的,例如

function syncJSON(i_url, callback) {
  $.ajax({
    type: "POST",
    async: false,
    url: i_url,
    contentType: "application/json",
    dataType: "json",
    success: function (msg) { callback(msg) },
    error: function (msg) { alert('error : ' + msg.d); }
  });
}

syncJSON("/pathToYourResouce", function (msg) {
   console.log(msg);
})