当ajax结果返回空值时显示错误消息


Display error message when ajax result return null value

我想问如何显示错误消息时,空值和0值在ajax结果从SQL

{"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');
        }
    }   
});

变量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');
    }   
});

试试这个,

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