【问题标题】:How to display error message if the ajax return 0 result from sql如果ajax从sql返回0结果如何显示错误消息
【发布时间】:2016-10-18 01:16:34
【问题描述】:

我试图显示一条错误消息来说明,当数据库(mysql)中的结果为 0 时 这将是一条错误消息。

下面的代码我尝试使用ajax来获取SQL结果,当它返回0时会显示错误。

$.ajax({
  type: "POST",
  url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
  dataType: "json", // serializes the form's elements.
  success: function (result) { 
  var chart = c3.generate({
       bindto: '#piepie',
       data: result.value,
       color: { 
         pattern: ['#f35213', '#f1af4c'] },
         pie: { title: "Productivity", }
     });

  },
  error: function() {

        if (result.percentage==undefined){ 
  alert ('Data are not ready yet!!');  
} else {
alert(result.percentage);
}
}   
});

【问题讨论】:

  • 有问题吗?您发布的代码不会在任何地方检查零。
  • @Charlie 我努力检查零,因为我得到的结果是这样的,{"percentage":[]} 空结果。所以我想在它返回空时弹出一个错误消息,并且在它得到值时没有弹出任何消息

标签: php mysql ajax4jsf error-messages-for


【解决方案1】:

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

试试这个

【讨论】:

  • 我的结果是空的像这样{"percentage":[]},我想在返回空结果时显示错误信息
  • 我改变了一点。试试看。我没有你的 json 结果,所以我无法测试这个。您应该能够使用 if 语句检查 0 或您想要的任何字符串来测试该值,如果为真则显示错误。
  • 我已尝试粘贴您的代码,但没有弹出错误消息。
【解决方案2】:
    if (result.percentage.length==0){ // THIS IS WHAT YOU NEED.
      alert ('ERROR!!');  
    } else {
     // YOUR CODE GOES HERE.

    }

正如您所评论的,您得到的结果为“{“percentage”:[]}”。所以你有百分比属性作为一个长度为 0 的数组。所以,你只需要测试它。

【讨论】:

  • 未捕获的类型错误:无法读取未定义的属性“长度”
  • 所以,这意味着你有未定义的百分比而不是数组。所以你可以检查 result.percentage==undefined 。此外,您应该在提问时粘贴您的回答。您可以使用 console.log(JSON.stringify(result)); 让您在浏览器的控制台中登录响应
  • 当我得到这样的结果时,{"percentage":["0.0","0.0","-50.0","-50.0","0.0","100.0"]," new":"0","pending":"0","completed":"1","ongoing":"1","delay":"1","prod":"14"} 它也会弹出向上错误信息
  • 我只想在百分比 = 未定义时弹出错误消息,并且我不知道百分比 = 值的任何弹出消息
  • 使用 == 符号进行比较。你必须写 if(result.percentage==undefined){ alert("ERROR"); } else { /* 代码 */ }
【解决方案3】:

试试这个!

if ((typeof result !== " undefined") && (result !== null) && (result == 0)){
    alert("YourErrorMessageHere"); 
}

【讨论】:

  • 我想在返回 undefined/null/0 时显示错误消息
  • 我已经稍微修改了内容,也许您可​​以选择使用其他诸如引导模式的东西来显示错误消息。希望这对你有用!
  • 当结果为[["More than 85%",null],["Less than 85%",0]]时,我要显示error else no error
  • 这么喜欢吗? if (result.More than 85% == "null"){ alert ('ERROR!!'); } 其他 {
  • 当结果为 {"value":{"columns":[["More than 85%","14"],["Less than 85%", 64]],"type":"pie"}}
猜你喜欢
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 2013-10-06
  • 1970-01-01
  • 2017-09-28
  • 2015-10-13
  • 2015-12-20
相关资源
最近更新 更多