dontay
dontay

Reputation: 73

Uncaught TypeError: Cannot read property 'status' of undefined when running ajax php

My response keeps getting a return as undefined.

My index.php has an event handler that fires off the following code

$.post("getData.php", onNewPost());

function onNewPost (response){

  if (response.status == "OK") {
     console.log(response);
  }

};//end new post

and My getdata.php is as follows

 <?php 
 $data = array("status" => "OK");

Upvotes: 0

Views: 409

Answers (1)

Regolith
Regolith

Reputation: 2982

Change your index.php to

$.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));

Here, first create the array in your getdata.php and json_encode that data. After that JSON.parse the returned data in your script, this will convert the data to JavaScript object. See here

Upvotes: 1

Related Questions