【问题标题】:Why does this constructor function not work? (In Ajax success)为什么这个构造函数不起作用? (在 Ajax 中成功)
【发布时间】:2017-07-19 21:42:46
【问题描述】:

我正在尝试提取我的 json 数据并将其放入一个变量中,该变量可从任何地方获得。但是我收到一条错误消息,上面写着:foods is undefined(最后的警报行)

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

【问题讨论】:

    标签: javascript jquery ajax object constructor


    【解决方案1】:

    只需尝试使用 new 关键字调用您的构造函数。它会起作用的。

    foods = new foodConstructor(data[0]);

    【讨论】:

      【解决方案2】:

      你忘记了新的关键字

      试试:

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

      【讨论】:

        【解决方案3】:

        Howard Fring 您要做的是将警报移动到一个函数中,并从 ajax 请求成功时调用的回调函数调用它。 food 在请求完成之前不会填充,这就是它未定义的原因。例如:

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

        通过在填充食物后在success 回调中调用foodAlert 将打开一个警报,并显示food.name 的值。

        【讨论】:

          猜你喜欢
          • 2022-01-04
          • 2014-08-24
          • 2016-08-15
          • 1970-01-01
          • 2016-06-28
          • 1970-01-01
          • 2017-07-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多