【问题标题】:req.body is null in expressjs server when sending post request from ionic app从 ionic 应用程序发送发布请求时,expressjs 服务器中的 req.body 为空
【发布时间】:2016-12-04 20:20:33
【问题描述】:

当我使用 angularjs $http.post 向我的 express js 服务器发送发布请求时,我正在开发一个 ionic 应用程序,我看不到 req.body 中的数据。

我在 localhost:3000 上运行我的服务器

我的服务器中用于 CORS 的代码

     app.use(function(req, res, next) {
     res.header("Access-Control-Allow-Origin", "*");
     res.header("Access-Control-Allow-Headers", "Content-Type");
     res.header("Access-Control-Allow-Methods", "GET,PUT,DELETE,POST");
     next();
     });

我的 Angular Js 发布请求

        $http.post('http://localhost:3000/signup',{"username":"x","password":"y"}).success(function(res){

     console.log(res);
     if(res.msg=="success")
     {
       //do something
     }
}

我可以将 req.body 中的数据视为“键”,例如:
{'{"username":"x","password":"y"}':''}
当我将离子应用程序的标题设置为:

$http.defaults.headers.post["Content-Type"] = 'application/x-www-form-   urlencoded; charset=UTF-8';

请告诉我如何调试

【问题讨论】:

  • 您在使用body-parser 吗?如果是,您是否添加了app.use(parser.urlencoded({ extended: true }));?
  • @georoot 我的 app.js app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false }));我也尝试了 app.use(bodyParser.urlencoded({ extended: true })) 但得到了相同的响应
  • 你的意思是说req.body 在你的情况下是空的?如果是,请检查 ajax 请求并将其发布,这样我们就可以看到参数是如何准确地发布到服务器的 :)
  • @georoot 这是我的 ajax 请求的样子
  • 看到这个答案——SO: Url Encoding in AngularJS front end

标签: angularjs node.js express ionic-framework


【解决方案1】:

您将获取整个数据作为req.body 中的键。这是因为您提出的角度要求是错误的。这是应该工作的代码部分

$http({
    url: 'http://localhost:3000/signup',
    method: "POST",
    data: { username : "a" , password : "b" }
})
.then(function(response) {
        // success
}, 
function(response) { // optional
        // failed
});

【讨论】:

  • 谢谢,但我的 ajax 请求在解析表单数据解析后采用这种格式 {"firstname":"a","lastname":"b","email":"abc@gmail .com","password":"12345678"}: 我在快递服务器上收到相同的正文
  • 所以我认为我无法从我的 ionic 应用程序中正确解析表单数据。任何建议我可以做些什么来改变它
  • 尝试以{ username : "a" , password : "b" }传递数据
  • 为您提供更多详细信息 Resposne Headers Access-Control-Allow-Headers:Content-Type Access-Control-Allow-Methods:GET,PUT,DELETE,POST Access-Control-Allow-Origin:*连接:保持活动内容长度:19 内容类型:应用程序/json; charset=utf-8 请求标头 Accept:*/* Accept-Encoding:gzip, deflate, br Accept-Language:en-US,en;q=0.8 Connection:keep-alive Content-Length:78 Content-Type:application/ x-www-form-urlencoded; charset=UTF-8 Host:localhost:3000 Origin:localhost:8100Referer:localhost:8100/?ionicplatform=android
【解决方案2】:

嗯,我想通了 我已经使用这个 stackoverflow 链接 Ionic framework http post request 在发送到我的服务器之前解析我的数据

  $http.defaults.headers.post["Content-Type"] = 'application/x-www-form-urlencoded; charset=UTF-8';
   Object.toparams = function ObjecttoParams(obj)
   {
     var p = [];
     for (var key in obj)
     {
       p.push(key + '=' + encodeURIComponent(obj[key]));
     }
     return p.join('&');
   };

   $http({
     url: 'http://localhost:3000/signup',
     method: "POST",
     data: Object.toparams(u)
   })
     .then(function(response) {
         console.log(response);
       },
       function(response) { // optional
         // failed
       });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    相关资源
    最近更新 更多