简体   繁体   中英

json return empty array

when my page loads, I'm using .ajax to calls server side code and return json. The issue 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM