【问题标题】:Display error message when ajax result return null value当ajax结果返回空值时显示错误信息
【发布时间】:2017-02-27 05:34:28
【问题描述】:

请问SQL ajax 结果为null 值和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');
        }
    }   
});

【问题讨论】:

    标签: php sql ajax error-messages-for


    【解决方案1】:

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

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

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

    【讨论】:

    • 当没有可用数据时,您确定 PHP 脚本返回 json_encode(null) 或 json_encode(0)?
    • 也许您应该测试if (result.value == null) 而不是if (result == null)?
    • {"value":{"columns":[["超过 85%",null],["小于 85%",0]],"type":"pie"} } 结果为 null 和 0
    • 那是result.value.columns[0][1] == null 和result.value.columns[1][1] == 0。
    • {"percentage":["-100","0","-50","-50","0"],"new":"0","pending": "0","已完成":"1","进行中":"1","延迟":"1"}
    【解决方案2】:

    试试这个,

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

    【讨论】:

    • 你能在这里发表你的回应吗?只是想看看服务器发送什么响应,据我建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 2018-10-31
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多