我想提取我的JSON數據並放入一個變量,這是從任何地方都可用。但我有一個錯誤信息,它說:食物是不確定的(警報排在最後)爲什麼這個構造函數不起作用? (在Ajax成功)
var foods;
function search() {
$.ajax({
url: "foodsrequest.php",
type: "GET",
dataType: "json",
async: false,
data: {"inputData": JSON.stringify(filterdata)},
success: function(data){
foods = foodConstructor(data[0]); ///yes, it is an array of objects and it has all the parameters needed
function foodConstructor(dataIn){
this.id = dataIn.id;
this.name = dataIn.name;
this.price = dataIn.price;
this.species = dataIn.species;
this.type = dataIn.type;
this.manufacturer = dataIn.manufacturer;
this.weight = dataIn.weight;
this.age = dataIn.age;
this.partner = dataIn.partner;
}
}
});
}
alert(foods.name);
謝謝,它的工作原理 –
歡迎您 –