1

我想问当SQL的ajax结果中的空值和0值时如何显示错误消息

{"value":
    {"columns": [["More than 85%",null],["Less than 85%",0]],
    "type":"pie"}
}

否则不显示弹出消息。

$.ajax({
    type: "POST",
    url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
    dataType: "json", 
    success: function (result) { 
        var chart = c3.generate({
            bindto: '#piepie',
            data: result.value,
            color: { 
                pattern: ['#f35213', '#f1af4c'] 
            },
            pie: { title: "Productivity", }
        });     
    },
    error: function() {
        if ((result == null) && (result == 0)){ 
            alert ('Data are not ready yet!!');  
        } 
        else {
            ('error');
        }
    }   
});
4

2 回答 2

1

该变量在函数result中不存在。error:您需要在success:函数中进行该测试。

和值在结构中很深null0您需要正确访问它们。

$.ajax({
    type: "POST",
    url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
    dataType: "json", 
    success: function (result) {
        if (result.value.columns[0][1] == null && result.value.columns[1][1] == 0) {
            alert ('Data are not ready yet!!');
        } else {
            var chart = c3.generate({
                bindto: '#piepie',
                data: result.value,
                color: { 
                    pattern: ['#f35213', '#f1af4c'] 
                },
                pie: { title: "Productivity", }
            });
        }
    },
    error: function() {
        alert('error');
    }   
});
于 2016-10-18T05:20:17.543 回答
0

尝试这个,

$.ajax({
   type: "POST",
   url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
   dataType: "json", 
   success: function (result) { 
      if ((result == null) && (result == 0)){ 
        alert ('Data are not ready yet!!');  
      }else {
          var chart = c3.generate({
               bindto: '#piepie',
               data: result.value,
               color: { 
               pattern: ['#f35213', '#f1af4c'] },
               pie: { title: "Productivity", }
          });
       }        
   },
   error: function() {
      alert('error'); 
   }   
 });
于 2016-10-18T05:18:36.463 回答