By using jquery ajax function, I can do something like:
$.ajax({
url: url,
type: \'GET\',
async: true,
dataType: \'json\',
data: data,
success:
One easy way to do this is to just use the jQuery always() function for your callback and then if you want to manually create an error just take advantage of Duck Typing!
For example:
$.get(url, {"data": data}).always(function(result)
{
if (typeof result.status !== "undefined" && result.status !== 200) // Status code 200 represents Success/OK
{
//handle error
error = "Your error was " + result.status + " " + result.statusText;
}
else
{
//success
}
});
The error section will always be run if there was a natural header error such as a 404 Not Found. If you want to manually return an error, you can just emulate the XHR object normally passed with an error (e.g. in PHP):
public function ServerSideAjaxResponse($data)
{
if (!$data)
{
$response["status"] = 1001; //Make up your own error codes! Yippee! Fun!
$response["statusText"] = "Error! You forgot to send the data!";
echo json_encode($return);
return;
}
}