1

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?

1 Answer 1

5

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;
Sign up to request clarification or add additional context in comments.

1 Comment

@ Nick Craver -- Ah ha! Yes, that did the trick Nick. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.