存储jQuery$.get函数将数据返回给类全局变量

存储jQuery$.get函数将数据返回给类全局变量,jquery,json,class,variables,Jquery,Json,Class,Variables,如何获取jQuery$。如何将检索到的json数据存储到类变量industry或product中 如果您查看从对php文件的$.get调用返回的数据,那么它确实会返回一个jSon数组,是的,我可以使用这些信息,但对我来说,更重要的是将返回的内容存储在类概要文件的全局变量中 Profile=new Profile("kazie.php"); Profile.getProfile(0123, 'Kayla', 'kayla@email.com'); var Profile = function(p

如何获取jQuery$。如何将检索到的json数据存储到类变量industry或product中

如果您查看从对php文件的$.get调用返回的数据,那么它确实会返回一个jSon数组,是的,我可以使用这些信息,但对我来说,更重要的是将返回的内容存储在类概要文件的全局变量中

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"
            );

    }
}
函数的文档将为您提供一些关于如何处理返回的JSON的想法(请注意,您不需要切换到此函数,如果您指定了“JSON”,它们是等效的。getJSON是一种方便的方法)。它比我解释得更好。

试试这个:

...
function (data){
          Profile.prototype.industry = data.industry
     }, "json"
);
...
这应该将行业存储到类的原型中。如果您不想将其存储到原型中(这将使其在所有实例中都可用),那么您只需要将其存储到您需要的实例中