【问题标题】:req.body return empty in app.delete functionreq.body 在 app.delete 函数中返回空
【发布时间】:2014-01-28 18:45:17
【问题描述】:

我正在尝试将一些 json 数据传递给 $http Delete 调用,但返回的 req.body 是一个空的 javascript 对象

这是我的 $http 删除调用,场景是一个 json 对象

    //Deletes the item from the database
   $http({ method: 'DELETE', url: 'http://localhost:3000/scenarios/' + scenario._id, data: scenario}).
       success(function (data, status, headers, config) {
         console.log(data);
            console.log('success');
        }).
        error(function (data, status, headers, config) {
            console.log('error');
        });

但在我对这个功能的明确回应中

exports.deleteScenario = function (req, res){
 var deletedScenario = req.body;
 console.log(deletedScenario);
...
}

在控制台中返回{}。那么如何在没有主体返回空对象的情况下将 JSON 数据传递给 http DELETE 调用呢?

【问题讨论】:

  • 对我有用——如果 HTTP 请求有一个实体主体,它会显示在 Express 中。您确定浏览器正在发送正文吗? (在浏览器的开发者工具网络面板中验证)

标签: javascript json node.js http express


【解决方案1】:

我知道现在问可能为时已晚,但你有没有指定 headers: {"Content-Type": "application/json;charset=utf-8"} 在您的 $http 请求配置中?

Http header 需要明确指定(直接使用 $http 时,如果您使用 angular $resource 则需要)其他 express 不会选择正确的解析器,然后导致空请求有效负载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 2022-01-11
    • 1970-01-01
    • 2021-08-17
    • 2021-05-05
    • 2016-11-20
    • 2022-01-23
    相关资源
    最近更新 更多