douye2488 2013-12-26 08:51
浏览 52
已采纳

在非对象上调用成员函数xxxx

Im trying to get data from an ajax call by creating an json object and parsing the object in my javascript.

The javascript function:

function confirm_unavailable_table(form) {
  event.preventDefault();
  var id = form.table_id.value;
  console.log(id);
  $.ajax({
    type: "GET",
    cache: false,
    url: "/kviberg/views/ajax_check_availability.php", 
    data: {id : id},
    dataType: "json",
    success: function(data) {
      //var obj = JSON.parse(data) || $.parseJSON(data);
      console.log(data);
    },
    error: function(data) {
      console.log(data);
    }
  });
}

The php file ajax_check_availability.php

require_once("../classes/reservation.php");

$id = intval($_GET['id']);
$res = Reservation::find_by_table_id($id);
$arr = array("id" => $id, "res_id" => 0, "sub_id" => 0); 
if($res) {
  //var_dump($res);  <-- Checked that outputs an object when just calling the php script 
  $res_id = intval($res->get_id());
  $arr["res_id"] = $res_id;
} 

echo json_encode($arr);

When I var_dump the $res object in php after just calling the script with the id = 20 (the object is in the database!) then I can see that the script actually works and outputs it with all the fields correct:

array(7) { [0]=> object(Reservation)#2 (14) { ... }

But when I run it through ajax and want to output it on the console like an JSON object I get this error:

![enter image description here][1]

The output from the log file says:

[26-Dec-2013 09:42:49 Europe/Berlin] PHP Fatal error:  Call to a member function 
get_id() on a non-object in
/Applications/MAMP/htdocs/kviberg/views/ajax_check_availability.php
on line 13
  • 写回答

1条回答 默认 最新

  • duandaodao6951 2013-12-26 09:04
    关注

    It seems that Reservation::find_by_table_id($id); returns an array.

    Try $res[0]->get_id()

    And $res = new Reservation(); is useless code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?