我從我的php頁面返回這個json,但jquery無法解碼它,結果始終爲空。jquery無法編碼json
{
"City": [
{
"CityID": "1",
"CityName": "istanbul"
},
{
"CityID": "2",
"CityName": "Ankara"
}
]
}
jQuery代碼:
$.getJSON("handlers/cityhandler.php", function(json){
var result = jQuery.parseJSON(json);
console.log(result[0].City.CityID);
Jquery的交替代碼:
$.getJSON("handlers/cityhandler.php", function(json){
$.each(json, function(i,City){
$("#selectcity").append('<option value="' + City.CityID + '">' + City.CityName + '</option>');
});
也許你需要寫'$。每次(json.City',而不是'$。每個(json' ? – Inferpse