I am writing an backbone js web app on top of an JSON server that returns JSON responses in JSend specification format.
Here are a few examples of that format:
GET /posts
{
"status": "success",
"data": {
"posts" [
{"id": 1, "title": "A blog post"},
{"id": 2, "title": "another blog post"}
]
}
}
POST /posts
{
"status": "fail",
"data": {
"title": "required"
}
}
By default the "error" event in $.ajax gets triggered by http codes, but since the JSend specification format does not use HTTP codes at all, I have to rewrite the $.ajax error handler.
The way it works by default (http codes):
$.ajax({
error: function() {
// Do your job here.
},
success: function() {
// Do your job here.
}
});
How can I rewrite the $.ajax error handler that it gets triggered when parsed the body and if the "status" property is "fail" or "error"?