Ajax字符串比较

时间:2016-05-20 15:49:09

标签: javascript jquery ajax string

我有一个基本的ajax功能

 setInterval(function() {
      var action = '';
      var status = '';
      $('#php-data').load('../Data/Dashboard.Data.php'); 
      $.ajax({type: 'POST', url: 'Data/Dashboard.Image.php', data: { json: action }, dataType: 'json' }).done(function(data) {
          console.log('Ajax Data: ' + data);
          status = JSON.parse(data);
          **if(status === 'false') {**
              console.log("script is NOT running");
              drawPieChart();
          } else {
              console.log("script is running");
            $('#php-image').load('../Data/Dashboard.Image.php'); //todo only load this is the ajax returned from Dashboard.image.php is true
          }
      });            
  }, 5000);

基本上我的ajax返回的唯一东西是“false”所以当我将状态与false比较时,它应该是true并且drawPieChart();但它始终运行jquery load()函数,即使我的ajax数据每次都返回false。 我也试过这个:

 setInterval(function() {
      var action = '';
      var status = '';
      $('#php-data').load('../Data/Dashboard.Data.php'); 
      $.ajax({type: 'POST', url: 'Data/Dashboard.Image.php', data: { json: action }, dataType: 'json' }).done(function(data) {
          console.log('Ajax Data: ' + data);
          status = data;
          **if(status === 'false') {**
              console.log("script is NOT running");
              drawPieChart();
          } else {
              console.log("script is running");
            $('#php-image').load('../Data/Dashboard.Image.php'); //todo only load this is the ajax returned from Dashboard.image.php is true
          }
      });            
  }, 5000);

和此:

 setInterval(function() {
      var action = '';
      $('#php-data').load('../Data/Dashboard.Data.php'); 
      $.ajax({type: 'POST', url: 'Data/Dashboard.Image.php', data: { json: action }, dataType: 'json' }).done(function(data) {
          console.log('Ajax Data: ' + data);
          **if(data == 'false') {**

              console.log("script is NOT running");
              drawPieChart();
          } else {
              console.log("script is running");
            $('#php-image').load('../Data/Dashboard.Image.php'); //todo only load this is the ajax returned from Dashboard.image.php is true
          }
      });            
  }, 5000);

1 个答案:

答案 0 :(得分:1)

if(status === 'false')

这表示一个字符串。请改用:

if(status == false)

你可以进一步简化它:

if(!status)