开发者

json return empty array

开发者 https://www.devze.com 2023-01-09 18:27 出处:网络
when my page loads, I\'m using .ajax to calls server side code and return json. The iss开发者_JAVA百科ue I\'m having is I get an empty array. So inside my \"success: function(data)\", it\'s saying dat

when my page loads, I'm using .ajax to calls server side code and return json. The iss开发者_JAVA百科ue I'm having is I get an empty array. So inside my "success: function(data)", it's saying data[0].id is undefined. Is there any way to handle this?


You can check the .length property before accessing it, return or do something else if it's 0, like this:

if(data.length === 0) {
  alert("empty!");
  return;
}
//it has entries, carry on...
var id = data[0].id;
0

精彩评论

暂无评论...
验证码 换一张
取 消