我是 jquery 和 JSON 的新手。我有以下 JSON 结构。
{
"address":{
"suburb":[
"HACKHAM WEST",
"HUNTFIELD HEIGHTS",
"ONKAPARINGA HILLS",
"m"
],
"state":"SA"
}
}
所以基本上以上就是对此的回应:
$.ajax({
type:'POST',
url: 'getAddress.php',
data:postCode='+postCode',
success: function(response) {
alert(response)
}
});
所以,我想要获取的是一个包含状态的变量和一个包含郊区的数组。
最佳答案
检查您是否有有效的 Ajax 请求:
$.ajax({
type: "POST",
url: "getAddress.php",
data: {
postCode : postCode // data as object is preferrable
},
dataType: "json", // to parse response as JSON automatically
success: function(response) {
var state = response.address.state;
var suburbs = response.address.suburb;
}
});
关于jquery - 尝试使用 jquery 迭代 JSON 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11212950/