This is sort of a basic question, but it's been nagging me for a while and I couldn't find any matching questions from my searches so far. So, apologies if it's too simple or repeated.
I have an API route (Express based) and I'm calling this route using jQuery AJAX call. Here is a simple stub to demonstrate the setup
$.ajax({
url: "http://localhost:3000/admin/items/" + itemId,
type: "POST",
dataType: "json",
data: itemData,
contentType: "application/json",
success: function(result){alert(result);},
error: function(xhr, ajaxOptions, thrownError){
alert("Could not update item details. Please try again later. Error: " + xhr.statusText);
}
});
From the server, I confirmed that the status code returned is 200 and here is the implementation:
collection.update({ _id : mongo.ObjectID(req.params.id) }, req.body, function(err, result) {
if(err){
console.log("Error is: " + err);
res.status(500).send("Could not update item with id: " + req.params.id);
}else {
if(result.result.n != 1){
res.status(500).send("Could not find item with id: " + req.params.id);
}
else{
console.log("Update successful");
res.status(200).send("Successfully updated item id: " + req.params.id);
}
}
});
The functionality works perfectly, but at the end of the call, the error function is getting called and the alert I'm getting is: "Could not update item details. Please try again later. Error: OK".I tried a simple res.send without explicitly setting the status code too, but somehow it invariably calls the error function. What am I missing?
via user1452030
No comments:
Post a Comment