javascript - 未捕获的类型错误 : Cannot read property 'status' of undefined when running ajax php

标签 javascript php ajax

我的响应不断收到未定义的返回。

我的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/

相关文章:

javascript - angularjs提交后清除输入字段

javascript - 非网络 Javascript 框架

javascript - MobX 自动运行只触发一次

javascript - 如何在我的 javascript 文件和 php 文件之间创建桥接代码以保护 MySQL 信息?

javascript - Leaflet Sidebar V2 设置默认打开

php - 如果没有数据库信息,则返回一个变量,用于创建有关无数据的注释

PHP SQLite PDO 检查用户是否在表中?

javascript - AJAX GET 竞争条件?

javascript - Ajax 调用(之前/成功)在 php 文件内不起作用

Jquery getJSON 不起作用,但 ajax 可以