【问题标题】:Post parameters are undefined in expresspost 参数在 express 中未定义
【发布时间】:2015-12-05 06:34:42
【问题描述】:

我正在学习用于 Web 开发的 MEAN 堆栈。我在客户端有以下代码:

var req = {
 method: 'POST',
 url: 'http://localhost:3000/Sad/3',
 headers: {
   'Content-Type': "Text"
 },
 data: {ID: Auth.sadit , UID: $scope.Sodid , PID: 1234}
};

以下是在服务器端:

app.post('/Sad/:Type', function(req, res) { 
        var param = req.data;
        var qry   = req.headers;
        var url   =req.url;
        console.log(req.data + "\n");
        if(req.params.Type == 1){
            <print all the parameters in request>
        }
        if(req.params.Type == 3){
            <Print the parameters in the request , change them and send it back>
            }

        res.status(200).send( " Hello User");
        });

现在,当我尝试打印从客户端发送的数据中的参数时,快递只给我未定义的。

【问题讨论】:

    标签: angularjs node.js express httprequest httpresponse


    【解决方案1】:

    看起来您正在向 api 发送 json,所以 content-type 应该是 application/json 而不是 text

    【讨论】:

    • 我也会使用 $http 或 $resource
    • 是的,我自己使用 $http。
    • 以下代码在客户端,我正在使用 http 服务 app.controller('cont', [ '$scope', 'AU', '$http', function($scope, AU , $http){ var req = { 方法: 'POST', url: 'localhost:3000/Sad/3', 标头: { 'Content-Type': "application/json" }, 数据: {ID: Auth.sadit , UID: $scope.Sodid , PID: 1234} }; $http(req).then( function(){ Auth.satid += 1; console.log("发送请求" + req.data); }, function() { console.log(" failed "); }); 在 wireshark 我看到 http 数据包携带这些值作为 jason 对象,但我无法在 express 中解析它们
    猜你喜欢
    • 2012-12-04
    • 2017-09-18
    • 2016-02-05
    • 2019-11-25
    • 2012-07-03
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多