javascript - return a PHP object to an ajax call -
i'm learning php oop, , getting used of these objects.
when create object in php file called via $.ajax
function, want deliver answer back. how supposed send object ajax call ? before oop, putting array, json_encode()
array, , worked perfectly. how adapt using oop?
thanks lot answers romain
example:
on client side
$.ajax( { url:"test.php", type:"post", datatype:"json", success:function(json) { // json template } });
on server side: test.php
require_once("bdd.php"); function loadclass($class) { require $class.".class.php"; } spl_autoload_register('loadclass'); $personnem = new personnemanager($db); $perso = $personnem->get("123456"); $perso = serialize($perso); // ???????????? header('content-type: application/json'); echo json_encode(array("result",$perso));
either use serialize or __tostring create json-representation of data can send down tube. not needing array around object.
however, there error in json parsers/interpreters can mess data when not wrapped in array. haven't heared of issue since couple of years should safe
Comments
Post a Comment