doujue1246 2014-05-11 04:11
浏览 147
已采纳

未捕获的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 2014-05-11 04:35
    关注

    return doesn't output anything in PHP, use echo

    $answer = array ( "result" => true, "message" => "Your event has been created");
    echo json_encode($answer);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python