Javascript 使用jquery ajax获取数据时显示错误消息

Javascript 使用jquery ajax获取数据时显示错误消息,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,我开始学习ajax。我有API。API返回两种类型的响应 1.当我有数据且系统中没有数据时成功 2.出现服务器错误时出现错误(异常) 在API文档中,我有如下内容 现场成功 类型-布尔 响应状态-正确/错误 现场数据 类型-对象 在数据对象中,我有其他字段。我想做的很简单。在系统中显示数据,在没有数据时显示错误表 根据文档,当数据在系统中时,这是一个正确的响应示例 { "success": true, "data": { "office_id": 1, "office_type"

我开始学习ajax。我有API。API返回两种类型的响应 1.当我有数据且系统中没有数据时成功 2.出现服务器错误时出现错误(异常)

在API文档中,我有如下内容

现场成功 类型-布尔 响应状态-正确/错误

现场数据 类型-对象

在数据对象中,我有其他字段。我想做的很简单。在系统中显示数据,在没有数据时显示错误表

根据文档,当数据在系统中时,这是一个正确的响应示例

{
"success": true,
"data": {
    "office_id": 1,
    "office_type": "s",
    "registrar_name": null,
    "registrar_mobile": null,
    "registrar_email": null
  }
}
{"success": true}
当系统中没有数据时,正确响应

{
"success": true,
"data": {
    "office_id": 1,
    "office_type": "s",
    "registrar_name": null,
    "registrar_mobile": null,
    "registrar_email": null
  }
}
{"success": true}

这就是我此刻得到的。当它在系统中找到mydata时,它会正确地显示字段。但当系统中并没有数据时,我就并没有定义。这个代码怎么了

$('#test').on('click', function(){
  $.ajax({
    dataType: 'json',
    type:"GET",
    url:"http://mywebsite.com/api/mydata",
    success: function(response, data) {
        console.log(data.data);
    },
    err: function(response, data) {
        console.log("no data");
    }
 });
});
你想要那个吗

success: function(response, data) {
    if(data.data == undefined)
    {
        //There is no data
    }
}

数据可用时使用

{
"success": true,
"data": [{
    "office_id": 1,
    "office_type": "s",
    "registrar_name": null,
    "registrar_mobile": null,
    "registrar_email": null
  }]
}
{
"success": true,
"data":[]
}
当数据不可用时,请使用

{
"success": true,
"data": [{
    "office_id": 1,
    "office_type": "s",
    "registrar_name": null,
    "registrar_mobile": null,
    "registrar_email": null
  }]
}
{
"success": true,
"data":[]
}
错误使用时

{
"success": false,
"error": {code:"111",message:"Error message"}
}
在客户端

检查如下

success: function(response, data) {
        var retrivedData=[];
        if(data.success && data.data.length){
         retrivedData=data.data;//or use data.data[0]
         }else if(data.success && data.data.length==0){
          console.log("No DATA")
          }else {
           console.log("Error in Data",data.error.message);
           }

    }

首先,将success函数更改为只有一个参数,因为第二个参数(本例中名为
data
)将是textStatus

然后,您可以检查响应的属性,以确定是否有数据或响应是否有错误

success: function (response) {
    if (response.err != undefined) {
        console.log("Error");
    }

    if (response.data != undefined) {
        console.log("There is data");
    } else {
        console.log("No data")
    }
}

请注意,在您的示例数据中,带有错误的响应仍然有
success:true
,这在我看来似乎有点愚蠢,因为如果有错误就不应该成功。

在我的例子中,通过循环,我解决了如下问题:

success: function(response) {

  if (response.length == 0) console.log('No data!');

  $.each(response, function (index, item) { 

   [...]

  }
}

这是因为当
success==false
时,没有名为
data
的属性,只有
success
err
。您希望得到什么?此处:
success:function(response,data)
-此回调函数仅获取作为第一个参数的响应,因此下一行中将不定义
data
。删除
数据
此处将下一行更改为
console.log(response.data)。但是,在检查response.data是否定义之前,请注意您的代码问题是:error not errit即使系统中有数据也会给我“no data”消息“if at the data in the system”我不知道您的意思,但是如果ajax请求产生错误,您应该检查端点、参数等。