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.Ahh... well have you thought about using a Javascript framework?
I use prototype to manage my AJAX calls on the javascript side.
In prototype.... when you make an AJAX call you can set two different functions to process the response: one for when the 200 SUCCESS code is returned and one for when 500 Internal Server error is returned.
Then server side, when I see an error I set the HTTP return code to 500 before I quit the script.
header("HTTP/1.1 500 Internal Server Error");
If you don't use a frame work.. you can still query for the HTTP status with something like this.
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200)
successHandler(xmlhttp.responseText);
else if (xmlhttp.status==500)
errorHandler(xmlhttp.responseText);
}Thanks a lot for the tip
errorHandler(xmlhttp.responseText);
Could you give me an example of a errorHandler function, please
Bye.Well say in your PHP script, you set the response to be: "Error. This action is evil"....
Then in your HTML, stick a div element to hold error/success messages.
<div id="error"></div>
Then in your javascript. Write an error handler which simply passes the error message generated by PHP into the innerHTML of that div.
Example:
function handleError(errorText)
{
element = document.getElementById(id);
element.innerHTML = errorText;
}
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.Ahh... well have you thought about using a Javascript framework?
I use prototype to manage my AJAX calls on the javascript side.
In prototype.... when you make an AJAX call you can set two different functions to process the response: one for when the 200 SUCCESS code is returned and one for when 500 Internal Server error is returned.
Then server side, when I see an error I set the HTTP return code to 500 before I quit the script.
header("HTTP/1.1 500 Internal Server Error");
If you don't use a frame work.. you can still query for the HTTP status with something like this.
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200)
successHandler(xmlhttp.responseText);
else if (xmlhttp.status==500)
errorHandler(xmlhttp.responseText);
}Thanks a lot for the tip
errorHandler(xmlhttp.responseText);
Could you give me an example of a errorHandler function, please
Bye.Well say in your PHP script, you set the response to be: "Error. This action is evil"....
Then in your HTML, stick a div element to hold error/success messages.
<div id="error"></div>
Then in your javascript. Write an error handler which simply passes the error message generated by PHP into the innerHTML of that div.
Example:
function handleError(errorText)
{
element = document.getElementById(id);
element.innerHTML = errorText;
}