即使响应正确,jQuery ajax调用后数据也不会更新

时间:2021-10-09 07:42:34

I have the following piece of code

我有以下代码

  var utility = function() {

      this.table = null;
      this.parsedData = null;
  }


  utility.prototype.fetchData = function(url, query) {

      var self = this;
      var params = null;

      if (query)
          params = query;

      $.ajax({
              url: url,
              data: params
          })
          .done(function(resp) {

              resp = JSON.parse(resp);

              if (resp.resp === 200) {

                  self.parsedData = resp.data;


              }

          });

      return this;

  };

I am calling it using an event handler below

我正在使用下面的事件处理程序调用它

   var utilityObj = new utility();

  $('#section').on('change', function() {

      var self = this;

      $.ajax({
          url: 'api/get_tests.php',
          method: 'GET',
          data: {
              section_id: this.value
          } 
      }).done(function(resp) {

          /* some other function calls here for dom manipulation */

          utilityObj.fetchData('api/get_sections_tasks.php', {
                  section_id: self.value
          });
      });

  });

On running the fetchData method I find that the parsedData still remains null and I haven't been able to figure out that I did wrong in my code. Any help would be really appreciated. Thanks.

在运行fetchData方法时,我发现parsedData仍然保持为null,我无法弄清楚我的代码中是否存在错误。任何帮助将非常感激。谢谢。

This is the response received on fetchData and it is correct

这是在fetchData上收到的响应,它是正确的

    {
    "resp": 200,
    "data": [{
        "Id": 1,
        "user_id": 1,
        "section_id": 1,
        "img": "\/uploads\/112015\/0b3eb1cc2a3473453b2be00091fc9a2f.jpg",
        "title": "0",
        "description": "dsafds",
        "workflow": "Doing",
        "created": "0000-00-00 00:00:00",
        "updated": "0000-00-00 00:00:00"
    }, {
        "Id": 2,
        "user_id": 1,
        "section_id": 1,
        "img": "\/uploads\/112015\/8bf91b2aa69222d8a337b98b67a486d0.jpg",
        "title": "0",
        "description": "sdafsdfa",
        "workflow": "Backlog",
        "created": "0000-00-00 00:00:00",
        "updated": "0000-00-00 00:00:00"
    }]
}

1 个解决方案

#1


0  

In this piece of code

在这段代码中

       var utilityObj = new utility();
 $('#section').on('change', function() {

      var self = this;

      $.ajax({
          url: 'api/get_tests.php',
          method: 'GET',
          data: {
              section_id: this.value
          } 
      }).done(function(resp) {

          /* some other function calls here for dom manipulation */

          utilityObj.fetchData('api/get_sections_tasks.php', {
                  section_id: self.value
          });
      });

  });

try to console.log(self.value) before that

在此之前尝试console.log(self.value)

utilityObj.fetchData('api/get_sections_tasks.php', {
                  section_id: self.value
          });

I think this is the problem. But i'm not sure.

我认为这是问题所在。但我不确定。

#1


0  

In this piece of code

在这段代码中

       var utilityObj = new utility();
 $('#section').on('change', function() {

      var self = this;

      $.ajax({
          url: 'api/get_tests.php',
          method: 'GET',
          data: {
              section_id: this.value
          } 
      }).done(function(resp) {

          /* some other function calls here for dom manipulation */

          utilityObj.fetchData('api/get_sections_tasks.php', {
                  section_id: self.value
          });
      });

  });

try to console.log(self.value) before that

在此之前尝试console.log(self.value)

utilityObj.fetchData('api/get_sections_tasks.php', {
                  section_id: self.value
          });

I think this is the problem. But i'm not sure.

我认为这是问题所在。但我不确定。