Per chiarire il concetto posto un po di codice
lato php
Codice PHP:
if(isset($_POST['nickname'])){
header('Content-type: text/html; charset=utf-8');
try {
$db= new DB($options);
$user= new User($db);
$user->insert($_POST['nickname']);
/*return a multidimensional array encoding with json_encode*/
echo $user->getData();
}
catch (InvalidArgException $e) {
/* return a json string */
echo json_encode('Sorry, the server is too busy');
error_log($e->getMessage()."\n", 3, $root."/log/error.log");
exit();
}
catch (DbException $e) {
/* return a json string */
echo json_encode('Sorry, the server is too busy');
error_log($e->getMessage()."\n", 3, $root."/log/error.log");
exit();
}
}
js
Codice PHP:
var user = eval("(" + xhr.responseText + ")");
if(typeof user=='string')
{
//something was wrong :(
}
else
{
//it works ;)
}