While creating a REST service with NodeJS I ran into a problem. The DELETE request gets interpreted as GET on the server side.
This may be some security feature to avoid data loss, but this moment I figure out what is wrong.
I am using express 4.13.4, es6-promise 4.0.5 and jquery 3.1.1
Client side:
deleteBear: function (bear) {
console.log("DEL REQ","http://domain.com/api/v1/" + bear._id);//CORRECT
var Promise = promise.Promise;
return new Promise(function (resolve, reject) {
$.ajax({
url: "http://domain.com/api/v1/" + bear._id,
method: "DELETE",
dataType: "jsonp",
xhrFields: {
withCredentials: false
},
headers: {
"Access-Control-Allow-Origin: ": "*",
"Access-Control-Allow-Methods: ": "DELETE",
"Access-Control-Allow-Headers: ": "Authorization",
"X-HTTP-Method-Override": "DELETE",
},
success: function(json){
console.log("OK",json);//OK GETS CALLED
resolve(json);
},
error: function(err){
console.log("ERR",err);
reject(err);
},
});
});
}
Server side:
router.route('/api/v1/:id')
/* GET request. */
.get(function(req, res) {
console.log("GET REQUEST");//THIS GETS CALLED INSTEAD OF DELETE
res.statusCode = 200;
res.jsonp("OK");
})
/* DELETE request. */
.delete(function(req, res) {
console.log("DELETE REQUEST");
res.statusCode = 200;
res.jsonp("OK");
});
No error gets shown in client console:
DEL REQ http://domain.com/api/v1/58060570b45c9340e916be90
OK OK
The server console shows GET being called instead of DELETE:
GET REQUEST
GET /api/v1/58060570b45c9340e916be90?callback=jQuery31107932283916014113_1476822027044&_=1476822027046 200 26.929 ms - 652976