如何在Javascript中读取数组值?
我收到了PHP的回复,我试图在JS上阅读,但没有得到值。我想得到responseCode的值。但它返回的是未定义的 响应代码: JS代码:如何在Javascript中读取数组值?,javascript,php,Javascript,Php,我收到了PHP的回复,我试图在JS上阅读,但没有得到值。我想得到responseCode的值。但它返回的是未定义的 响应代码: JS代码: 只需在php代码中使用“json_encode”并返回请求,即json中的数组您应该将数组编码为json并将该json发送给客户端。我无法读取json。我正通过这种方式尝试JSON.parse(data.responseCode)并返回错误Uncaught SyntaxError:JSON中的意外标记u位于位置0{“request”:null,“receip
只需在php代码中使用“json_encode”并返回请求,即json中的数组您应该将数组编码为json并将该json发送给客户端。我无法读取json。我正通过这种方式尝试JSON.parse(data.responseCode)并返回错误Uncaught SyntaxError:JSON中的意外标记u位于位置0{“request”:null,“receiptId”:“123555”,“responseCode”:“0”,“message”:“PSI-3217:达到的最大尝试次数”。,“transTime”:“4\/2\/2021 9:21:38 PM”,“merchantEmail”:”john@gmail.com“,“merchantId”:“10”}@felixkligf如果没有将jQuery配置为自动解码JSON,则必须执行
JSON.parse(data).responseCode
。不管怎么说,副本显示了该做什么。我无法读取JSON。我正通过这种方式尝试JSON.parse(data.responseCode)并返回错误Uncaught SyntaxError:JSON中的意外标记u位于位置0{“request”:null,“receiptId”:“123555”,“responseCode”:“0”,“message”:“PSI-3217:达到的最大尝试次数”。,“transTime”:“4\/2\/2021 9:21:38 PM”,“merchantEmail”:”john@gmail.com“,“merchantId”:“10”}@SaadMasood修复了这个问题:console.log(data.responseCode);返回未定义的console.log(data.responseCode)@SaadMasood您还必须插入数据类型:“json”,
Array
(
[request] =>
[receiptId] => 11233-555
[responseCode] => 0
[message] => PSI-3217:Maximum number of attempts reached.
[transTime] => 4/2/2021 8:50:09 PM
[merchantEmail] => johndoe@gmail.com
[merchantId] => 19
)
$.ajax({
method: "POST",
url: "https://john.com/ca/pay.php",
data: objData,
success: function (data, status) {
console.log(data['responseCode']);
},
error: function (error) {
}
})