当 ajax 结果 return 为空值时显示错误信息

Display error message when ajax result return null value

请问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');
        }
    }   
});

试试这个,

$.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'); 
   }   
 });

变量 resulterror: 函数中不存在。您需要在 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');
    }   
});