如何让jQuery $.get将检索到的json数据存储到类变量行业或产品中?
如果您查看从 $.get 调用到 php 文件返回的数据,那么它确实返回了一个 jSon 数组,是的,我可以使用这些信息,但对我来说更重要的是将返回的内容存储在类 Profile 的全局变量中。
Profile=new Profile("kazie.php");
Profile.getProfile(0123, 'Kayla', 'kayla@email.com');
var Profile = function(php_file){
this.php_file=php_file;
this.serial;
this.industry;
this.product;
//Also need to access scope as well
};
Profile.prototype={
getProfile:function(serial, name, email){
$.get(this.php_file,{action: "retrieve_profile", serial: serial, name: name, email: email},
function (data){ //Return a json array of our new profile
//Data is an array of properties. array("industry"=>"Retail");
//I need to assign these properties to my global variables for
//***How to I get data["industry"] to store in Profile.industry?**
}, "json"
);
}
}
.getJSON 函数的文档将为您提供一些有关如何处理返回的 JSON 的想法(请注意,您不需要切换到此函数,鉴于您指定了"json",它们是等效的,.getJSON 是一种方便的方法)。它比我能更好地解释它。
试试这个:
...
function (data){
Profile.prototype.industry = data.industry
}, "json"
);
...
这应该将行业存储到类的原型中。如果您不想将其存储到原型中(这将使它在所有实例中都可用),那么您只需将其存储在您需要的实例中。