phunkrebel
phunkrebel

Reputation: 73

Error handling for Ajax request in express.js

I can't seem to find out how to print a custom error message in alert().

I found this in the express guide

app.use(function(err, req, res, next){

if (req.xhr) {
    res.send(500, {error: 'Oops'});
} else {
    next(err);
}

});

I got only to print the error code like this:

request.fail(function(jqXHR, textStatus, err) {
    alert(err);
});

But how to i print 'Oops' or any other message that bubbles up to my custom middleware?

Thanks in advance

Upvotes: 3

Views: 1668

Answers (1)

robertklep
robertklep

Reputation: 203529

You can parse it out of jqXHR.responseText:

request.fail(function(jqXHR, textStatus, err) {
  alert(JSON.parse(jqXHR.responseText).error);
});

Upvotes: 1

Related Questions