我的响应不断收到未定义的返回。
我的index.php
有一个事件处理程序,它会触发以下代码
$.post("getData.php", onNewPost());
function onNewPost (response){
if (response.status == "OK") {
console.log(response);
}
};//end new post
我的getdata.php
如下
<?php
$data = array("status" => "OK");
最佳答案
将您的index.php更改为
$.post("getData.php", onNewPost);
function onNewPost (response){
// console.log(response);
response_parse = JSON.parse(response);
if (response_parse.status == "OK") {
console.log(response);
}
};//end new post
getdata.php
$data = array("status" => "OK");
die(json_encode($data));
在这里,首先在 getdata.php 中创建数组并 json_encode
该数据。
在脚本中返回 JSON.parse 数据之后,这会将数据转换为 JavaScript 对象。 See here
关于javascript - 未捕获的类型错误 : Cannot read property 'status' of undefined when running ajax php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56420475/