gpt4 book ai didi

javascript - 如何访问ajax返回的结果数组?

转载 作者:行者123 更新时间:2023-11-28 08:52:08 25 4
gpt4 key购买 nike

嗯!!我的工作完美无缺,现在我又开始用头敲击键盘了。

我想访问数组内定义的列,但我未定义,但如果我使用下面代码片段中详细介绍的警报显示结果,我会看到以下内容:

[{"firstname":" Mr","0":" Mr","lastname":" Two","1":" Two","user_public_info_id":"3","2":"3","st_usage_id":null,"3":null},{"firstname":" Mr","0":" Mr","lastname":" Three","1":" Three","user_public_info_id":"5","2":"5","st_usage_id":null,"3":null}] 
***
g
***
e
***
undefined

这是 Ajax 代码:

$.ajax({
type: "POST",
url: "models/ajaxHandler.php",
data: "handler=getStActivitySharingList&stu_id="+stu_id,
datatype: "json",
success: function(result){
var count = 0;
if (result !== null)
{
//display results
alert(result + " <br />*** <br />" + result[0] +" <br />*** <br />" + result[1] + " <br />*** <br />" + result[0]["firstname"]);

//clears choice list
clearOptions();

//result = $.parseJSON(result); //if this is used cannot display result again
alert (result);

$.each(result, function (i, elem) {
alert("elem"+elem.st_usage_id ); //displays as undefined and won't break
if (elem.st_usage_id === null)
{
count++;
alert(elem.firstname + " "+ elem.lastname + " " + elem.user_public_info_id);
appendOption(elem);
}
});
}
alert(count);
if (count === 0){
noResultsAvailableOption();
}else{
resultsAvailableOption();
}

ShowDialog(false);
e.preventDefault();
},
error: function(){
alert("ajax failure: could not retrieve a list of contacts");
}
});

最佳答案

我不知道你如何从 PHP 返回它,但在 jquery 中尝试:

sucess: function (result)
{
console.log(JSON.parse(result)); // parse string to json
}

参见json.org

关于javascript - 如何访问ajax返回的结果数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19012689/

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