Uncaught TypeError: Unable to read property 'result' of null

advertisements

Im trying to create an event by clicking a button which will submit three pieces of information that I provide. My PHP script is supposed to return a JSON encoded response of the success or failure of this event creation. An example of such a response is:

  {
    "result":true,
    "message":"Your event has been created"
  }

To return such a response, I have my PHP script doing the following:

  $answer = array ( "result" => true, "message" => "Your event has been created");
  return json_encode($answer);

When I click on the button to create the event on the web page, the developers console in Chrome displays the following message: Uncaught TypeError: Cannot read property 'result' of null. I've played around with the way I return that response but nothing has worked so far.

FYI, additional ajax code kicks in to determine what to do next once the response is received but based on the error, it seems that there is something wrong with the way I am returning the response.

Edit: Ajax code that will handle the returned response:

  // if that response is good
  if(response.result) {
        $('#event-create').dialog('close');
        $('#calendar').fullCalendar('refetchEvents');
  // otherwise error
  } else {
  $('#error-message').html(response.message).dialog('open');

EDIT 2: Echoing it out seemed to resolve the issue and there are no errors being thrown at the user. There is one more issue though left to be resolved that is listed in the developers console now though. Its the exact same error however it cannot read property 'length' of null. Unfortunately, I am not sure what its referring to in this case.


return doesn't output anything in PHP, use echo

$answer = array ( "result" => true, "message" => "Your event has been created");
echo json_encode($answer);