doujue1246
doujue1246
2014-05-11 04:11

未捕获的TypeError:无法读取null的属性“result”

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongwu3747 dongwu3747 7年前

    return doesn't output anything in PHP, use echo

    $answer = array ( "result" => true, "message" => "Your event has been created");
    echo json_encode($answer);
    
    点赞 评论 复制链接分享
  • dongyin0628 dongyin0628 7年前

    Echo it out:

    echo json_encode($answer);
    
    点赞 评论 复制链接分享

相关推荐