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) { … }