简体   繁体   English

为什么此构造函数不起作用? (在Ajax成功中)

[英]Why does this constructor function not work? (In Ajax success)

I'm trying to extract my json data and put into a variable, which is available from everywhere. 我正在尝试提取我的json数据并将其放入一个变量中,该变量随处可见。 But I've got an error message, it says : foods is undefined (the row of the alert in the end) 但是我收到一条错误消息,它说:食物未定义(最后一行警报)

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

You forgot the new keyword 您忘记了新关键字

Try: 尝试:

            foods = new 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 ); 

Just try invoking your constructor with new keyword. 只需尝试使用new关键字调用您的构造函数即可。 It will work. 它会工作。

foods = new foodConstructor(data[0]);

Howard Fring what you are going to want to do is move the alert into a function and call it from the call back function that is called when the ajax request succeeds. Howard Fring您要做的是将警报移到一个函数中,并从ajax请求成功时调用的回调函数中调用它。 food is not populated until the request is finished that is why it is undefined. 在请求完成之前,不会填充food ,这就是未定义food原因。 For example: 例如:

var foods;
      function search() {
        $.ajax({
          url: "foodsrequest.php",
          type: "GET",
          dataType: "json",
          async: false,
          data: {"inputData": JSON.stringify(filterdata)},
          success: function(data){

            foods = new 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;
            }
            foodAlert();
          }
        });
      }

      function foodAlert(){
        alert(foods.name);
      }

By calling foodAlert in the success call back after food is populated will open an alert with the value of food.name shown. success填充食品后,通过在success调用中调用foodAlert ,将打开一个警告,显示显示的food.name值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM