whisher06
03-26-2007, 07:06 AM
Hi.
I've got some troubles managing exceptions/errors
with ajax.
Now I'm using this method:
Php side:
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 side:
var user = eval("(" + xhr.responseText + ")");
if(typeof user=='string')
{
//something was wrong :(
}
else
{
//it works ;)
}
Have you got any tips ?
Thanks in advance.
I've got some troubles managing exceptions/errors
with ajax.
Now I'm using this method:
Php side:
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 side:
var user = eval("(" + xhr.responseText + ")");
if(typeof user=='string')
{
//something was wrong :(
}
else
{
//it works ;)
}
Have you got any tips ?
Thanks in advance.