【发布时间】:2021-07-05 21:06:20
【问题描述】:
我正在尝试从 php 文件中检索 json 输出并输出一个值。有人能告诉我如何检测值是否未定义以及如何访问数组中的值之一吗?
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);
}
}
});
【问题讨论】:
-
您遇到了什么错误?你的代码似乎正确
标签: javascript arrays json element