Javascript ajax调用async:false [英] Javascript ajax call async:false

查看:101
本文介绍了Javascript ajax调用async:false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个从服务器接收数据的AJAX调用。我发现的唯一解决方案是使async:false以获取函数外的数据。但它被弃用了。有没有更好的选择来获得函数的变量?任何建议将不胜感激。



I have this AJAX call that receives data from the server. The only solution I've found is to make async: false to get the data outside the function. But it is deprecated. Is there any better option to get the variable out the function? Any suggestion would be appreciated.

var suggest =[];
    $.ajax({
        type: "POST",
        url: //LINK,
        async: false,
        data: {
            "dataA": string
        },
        cache: false,
        success: function(data) {
            suggest = JSON.parse(data);
        }
    });





我尝试了什么:



我正在使用我从AJAX调用多次获取的变量有没有更好的方法从外面的成功函数调用数据?





What I have tried:

I am using the variable i'm getting from the AJAX call many times is there any better way to do call the data from the success function outside?

for (var i = suggest.length - 1; i >= 0; i--) {

       for (var j = 0; j < arrString.length; j++) {
           if (suggest[i] === arrString[j]) {
               suggest.splice(i, 1);
           }
       }
   }

推荐答案

.ajax({
键入:POST,
url:// LINK,
async:false,
data:{
dataA:string
},
cache:false,
success:function(data){
suggest = JSON.parse(data);
}
});
.ajax({ type: "POST", url: //LINK, async: false, data: { "dataA": string }, cache: false, success: function(data) { suggest = JSON.parse(data); } });





我尝试了什么:



我正在使用变量我是多次从AJAX调用中获取是否有更好的方法从外部成功函数调用数据?





What I have tried:

I am using the variable i'm getting from the AJAX call many times is there any better way to do call the data from the success function outside?

for (var i = suggest.length - 1; i >= 0; i--) {

       for (var j = 0; j < arrString.length; j++) {
           if (suggest[i] === arrString[j]) {
               suggest.splice(i, 1);
           }
       }
   }


这个怎么样?

how about this?


.ajax({
type:POST,
url:// LINK,
async:false,
数据:{
dataA:字符串
},
缓存:false,
成功:处理
});
功能程序(jsondata){
...
}
.ajax({ type: "POST", url: //LINK, async: false, data: { "dataA": string }, cache: false, success: process }); function procees(jsondata) { … }


这篇关于Javascript ajax调用async:false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆