jQuery.post() function fail
After installing a newer jquery version the jQuery.post() failed. The documentation says not to use the deprecated jqXHR.success()
, jqXHR.error()
, and jqXHR.complete()
callback methods. As of jQuery 1.8 prepare the code with jqXHR.done()
, jqXHR.fail()
, and jqXHR.always()
instead.
But on every post action the callback fail was called, the request send to the url was working fine.
function doPostSomething(key, val) {
var req = $.post("url/something", { key: key, value: val }, "json");
req.fail(function() { alert("There is an error\n" + status); });
req.done(function() {
doSomeStuff() ;
});
}
The reason why jquery handles it as an error is I post something without requesting data. I just store something in a database (key, val).
The url serves a tomcat application with the following response status code in a servlet:
if (productsDAO.getResult() != null) {
response.sendError(460, productsDAO.getResult());
} else {
// response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
Changing the response status code to SC_NO_CONTENT will work.