Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning json data from php to ajax

I'm trying to get a json object from php so I can work with it in ajax. Here is my ajax code:

 $.ajax({
   type: 'get',
   url: eventsListPath,
   dataType : "json",
   data: {},
   success: function (data) {
       $('#eventInformation').html(data.table);
       alert(data.table);
   }
});

And my PHP:

$obj->table="hey";
echo json_encode($obj, JSON_UNESCAPED_SLASHES);

But the line

alert(data.table);

comes back with 'undefined'. Any ideas?

like image 531
Steven Jacks Avatar asked Oct 31 '22 21:10

Steven Jacks


1 Answers

Try this in your php code. Json encode an array.

$obj['table']="hey";
echo json_encode($obj, JSON_UNESCAPED_SLASHES);

Alternate - Or your class should be like this

class your_classname
{
  public $table;
 //other class related code
}
$obj = new your_classname;

$obj->table="hey";
echo json_encode($obj, JSON_UNESCAPED_SLASHES);
like image 178
Harish Lalwani Avatar answered Nov 04 '22 08:11

Harish Lalwani