Reputation: 13721
Can someone help me with sending an error message from the server to client side via ajax? For example, if my server side returns a 404 error, I want it to also send a custom message: 'something is wrong'
server.js
api.get('/api/some_route', function(req, res, next) {
res.sendStatus(404, 'something is wrong');
});
client.js
$.ajax({
type: "POST",
data: data,
success: function(data, status) {
console.log(status);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus) //returns error
console.log(errorThrown) //returns bad request
//I want to someohow print the 'something is wrong' message from the api
}
});
Thanks in advance!
Upvotes: 1
Views: 1108
Reputation: 19070
You just have to call the status method before calling send
or json
:
res.status(404).send({ error: "Something is wrong"});
Upvotes: 1