By using jquery ajax function, I can do something like:
$.ajax({
url: url,
type: \'GET\',
async: true,
dataType: \'json\',
data: data,
success:
This original question was posted years ago, and perhaps there are better ways of handling this now. Here's a suggestion to handle multiple types of errors, including the OP's question.
function handleError (type){
type = type || "Connection Issue";
var value = "Error occurred: " + type;
alert(value);
}
function checkIfError(response){
if (response == "DB_ERROR") {
handleError("Server error");
throw new Error("JS died"); // optional, keep from running additional script
}
}
var example = {
success: function (response) {
checkIfError(response); // check data if error msg
// do stuff when proper data sent
alert(response);
}, error: handleError // connection error, call error msg
};
test1 = example['error']();
function handleError (type){
type = type || "Connection Issue";
var value = "Error occurred: " + type;
alert(value);
}
function checkIfError(response){
if (response == "DB_ERROR") {
handleError("Server error");
throw new Error("JS died"); // optional, keep from running additional script
}
}
var example = {
success: function (response) {
checkIfError(response); // check data if error msg
// do stuff when proper data sent
alert(response);
}, error: handleError // connection error, call error msg
};
test2 = example['success']("DB_ERROR");
function handleError (type){
type = type || "Connection Issue";
var value = "Error occurred: " + type;
alert(value);
}
function checkIfError(response){
if (response == "DB_ERROR") {
handleError("Server error");
throw new Error("JS died"); // optional, keep from running additional script
}
}
var example = {
success: function (response) {
checkIfError(response); // check data if error msg
// do stuff when proper data sent
alert(response);
}, error: handleError // connection error, call error msg
};
test3 = example['success']("Proper Response");
By using functions, if you have multiple calls in your scripts, you can simplify changes.
Have your PHP (or whatever server side code) return the response of "DB_ERROR" (or whatever you want) to trigger the error; allows you to do different things depending on the error.