我在ajax调用下使用从服务器端获取json对象(php)
$.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)时,当我尝试在控制台上打印任何元素时,它也不起作用,它给了我未定义的
答案 0 :(得分:1)
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");
}
});