gpt4 book ai didi

javascript - JSONP 接收回不可解析的 JSON

转载 作者:行者123 更新时间:2023-11-30 06:03:14 24 4
gpt4 key购买 nike

接收回不可解析的 JSON 响应。我很好地收到了 JSON,但它采用的是这种格式,我该如何解析并说出每行都包含值的警报。非常感谢!

请求开始

$.ajax({
url: apiURL+'getLocations/category/'+ category +'/countyId/'+ countyId +'/searchString/'+ searchString +'?callback=?',
contentType: 'application/json',
crossDomain: true,
dataType: 'jsonp',
callback: callTest1(),
complete: function(data){
if(data.code != '400')
{
$.each(data, function(key, value) {
$.each(value, function(key, val){
alert(data.id);
})
});
}
},
success: callTest2()
});

请求结束

输出开始

{"Location":[{"id":0,"postcodeId":85,"categoryId":0,"location":
"Enfield Island Village"},{"id":0,"postcodeId":44,"categoryId":0,
"location":"Isle of Dogs"},{"id":0,"postcodeId":269,"categoryId":0,
"location":"Isleworth"},{"id":0,"postcodeId":135,"categoryId":0,
"location":"Islington"}]
}

输出端

最佳答案

试试这个。

var data = {"Location":[{"id":0,"postcodeId":85,"categoryId":0,"location":
"Enfield Island Village"},{"id":0,"postcodeId":44,"categoryId":0,
"location":"Isle of Dogs"},{"id":0,"postcodeId":269,"categoryId":0,
"location":"Isleworth"},{"id":0,"postcodeId":135,"categoryId":0,
"location":"Islington"}]
};

$.each(data.Location, function(index, val){
alert(val.id + " , " + val.postcodeId + " , " +val.categoryId + " , " +val.location );
});

我认为您缺少 .Location 部分。

如果您对硬编码值不满意,请试试这个。

$.each(data.Location, function(index, datum) {
$.each(datum, function(key, value) {
alert(key + " : " + value.toString());
});
});

工作演示:http://jsfiddle.net/naveen/kLEA6/

关于javascript - JSONP 接收回不可解析的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6990529/

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