I'm attempting to retrieve json output from a php file and output a value. Can someone enlighten me on how to detect if the value is undefined and how to access one of the values in the array?
php response:
{"myObj":[{"userid":"9000","name":"Foo"},{"userid":"1000","name":"Bar"}]}
$.ajax({
type: 'POST',
url: 'reply.php',
beforeSend: function (xhr) {
if (xhr && xhr.overrideMimeType) {
xhr.overrideMimeType('application/json;charset=utf-8');
}
},
data: {
"action": "getmyObj"
},
success: function (reply) {
if (reply.myObj.length > 0) {
for (i = 0; i < 3; i++){
if (typeof reply.myObj[i].name === 'undefined') {
console.log("Undefined");
} else {
alert(reply.myObj[i].name);
}
}
});