问题描述
我正在使用expressjs和body-parser中间件.
I'm using expressjs and the body-parser middleware.
这是我的启动方式:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
我从客户端发送一个DELETE请求,当我尝试从服务器端接收它时,我得到一个空对象:
From the client I'm sending a DELETE request and when I try to pick it up from the server side I get an empty object:
app.delete('/', function(req, res) {
console.log(util.inspect(req.body)); //outputs {}
//some more code
});
但是,当我通过POST发送时,我得到了我需要的东西:
however when I send it with a POST I get what I need:
app.post('/delete', function(req, res) {
console.log(util.inspect(req.body)); //outputs { mid: 'ffw1aNh2' }
//some more code
});
值得注意的是,我没有在客户端(angularjs)上进行任何更改,但是方法和url以及firefox网络调试器显示了两种情况下发送的数据.
It is worth noting that I don't change anything on the client side (angularjs) but the method and the url and the firefox network debugger shows the data being sent in both situations.
这里缺少什么?为什么在删除方法上会得到一个空的正文对象?
What am missing here? Why am I getting an empty body object on a delete method?
推荐答案
$ http服务源代码(使用$ http的DELETE请求)不允许在请求的正文中发送数据.
The $http service source code, a DELETE request using $http does not allow for data to be sent in the body of the request.
DELETE请求的规范对于是否应允许请求正文有些含糊,但Angular不支持.
The spec for a DELETE request is somewhat vague on whether or not a request body should be allowed, but Angular does not support it.
使用此
$httpProvider.defaults.headers.delete = { "Content-Type": "application/json;charset=utf-8" };
然后
$http.delete(url, { data: data })
这篇关于使用express和body-parser解析DELETE请求时body为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!