我想從外部URL填充json數據的我的選擇框。Ajax未捕獲TypeError:無法讀取未定義的屬性「長度」
我不斷收到
下面Uncaught TypeError: Cannot read property 'length' of undefined
是我的JSON響應
{"ecoachlabs":{
"status":"201",
"msg":"Form data loaded successfully.",
"categories":[
{"id":"2","category":"church"},{"id":"3","category":"financial institution"},
{"id":"4","category":"old students association"},
{"id":"1","category":"school"},
{"id":"5","category":"tertiary"}
],
"storage":[
{"id":"1","category":"100MB"},{"id":"2","category":"250MB"},
{"id":"3","category":"500MB"},{"id":"4","category":"2GB"},
{"id":"5","category":"3GB"},{"id":"6","category":"5GB"}
]
}}
下面
是我的ajax郵編
$(document).ready(function() {
$("#institution_category").click(function() {
var formData = {
load_request_form_data: "1"
}; //Array
$.ajax({
url: "http://api.ecoachlabs.com/v1/requests/api.php",
type: "POST",
data: formData,
success: function(data, textStatus, jqXHR) {
console.log(data.ecoachlabs.categories); //data - response from server
$.each(data.categories, function(i, v) {
$('#institution_category').append($('<option value="' + v.id + '">' + v.category + '</option>'));
});
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
});
});
確實'data.categories'存在嗎?在使用'data.ecoachlabs.categories'之前的行中。如果你給'$ .each'一個空或非數組值,你有時可能會得到這個錯誤 –