gpt4 book ai didi

jquery 异步和 JSON 数据

转载 作者:行者123 更新时间:2023-12-03 22:58:58 25 4
gpt4 key购买 nike

javascript jquery and using eval我仍然无法让jquery异步读取数据。

 data1=[1,2,3,4]

注意:我在下面的示例中包含了 async:true 只是为了显示差异

下面的示例返回“null”

$(document).ready(function(){

var myArray=[];
myArray=getValues();
alert(myArray);
function getValues(){
var result=null;
$.ajax({
url: 'data1.html',
type: 'get',
dataType: 'json',
cache: false,
success: function(data) {result = data;},
async:true,
});
return result;
};
})

下面的示例工作正常,并以数组形式给出结果,即 [1,2,3,4]

$(document).ready(function(){

var myArray=[];
myArray=getValues();
alert(myArray);
function getValues(){
var result=null;
$.ajax({
url: 'data1.html',
type: 'get',
dataType: 'json',
cache: false,
success: function(data) {result = data;},
async:false,
});
return result;
};
})

有人可以解释一下如何异步获取结果吗谢谢

最佳答案

我会把它改成这样......

$(document).ready(function(){

function postProcessing(data) {
var myArray = data;
alert(myArray);
}


getValues();

function getValues(){
$.ajax({
url: 'data1.html',
type: 'get',
dataType: 'json',
cache: false,
success: postProcessing,
async:true,
});
};
})

关于jquery 异步和 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5644451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com