gpt4 book ai didi

javascript - 使用ajax成功方法访问json中的特定元素

转载 作者:行者123 更新时间:2023-11-30 16:02:44 25 4
gpt4 key购买 nike

我正在使用下面的 ajax 调用从服务器端 (php) 获取一个 json 对象

$.ajax({
url: url,
method: "POST",
data: { cdrid : cdrid },
dataType: "json",
success: function(data)
{
$("#appName").val(data.appname);
$("#callDate").val(data.callDate);
$("#editHavenRecord").modal("show");
},
error: function()
{
alert("Unable to get haven data");
}
});

我正在从服务器获取以下 json 对象作为响应

[{"id":"433","cdrid":"5339922","appid":"108","appname":"Haven Holidays(Service) 2009","dnis":"1172441753","CallDate":"2016-03-01","CallInTime":"2016-03-01 15:08:28","CallEndTime":"2016-03-01 15:11:25","TranscribeDateTime":"2016-03-02 05:16:56","agentid":"K9Q","calloutcome":"Good Record","TermCd":"GR","title":"Mr","fname":"Elwood","lname":"Ward","Addr1":"99 Enclingson Road","Addr2":"Bournemouth","Addr3":"","Country":"","city":"","town":"","postcode":"TH9 1HR","HeardAdvert":"BRAND_TV","ver_code":"VO","ver_date":"3\/2\/2016 5:26:32 AM","Duration":"177","HangUpCode":"8010","TranscribedBy":"Tom.Rodie   ","batchid":"903"}]

但是,当我尝试访问任何特定元素(如 data.appname)时,它也不起作用,当我尝试在控制台上打印任何元素时,它给我未定义

最佳答案

data是里面一个元素的数组,它的元素就是你要的对象。所以数据[0]

$.ajax({
url: url,
method: "POST",
data: { cdrid : cdrid },
dataType: "json",
success: function(data)
{


// data is an array of one element inside, its element is the object you want. so data[0]
$("#appName").val(data[0].appname);
$("#callDate").val(data[0].callDate);
$("#editHavenRecord").modal("show");


},
error: function()
{


alert("Unable to get haven data");

}
});

关于javascript - 使用ajax成功方法访问json中的特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37472593/

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