【问题标题】:http post password to node.js server from angularhttp从角度向node.js服务器发布密码
【发布时间】:2016-08-08 09:28:12
【问题描述】:

这让我很烦。我知道我可以做到以下几点:

$http.post('https://url:8080/api/registerUser?username=' + usrName + '&usrPassword=' + usrPassword);

但我猜这不是向服务器传递密码的正确方法。所以我试过这个:

$http({
    method: 'POST',
    url: url2,
    data: {
        userName: encodeURIComponent(usrName), 
        userPassword: encodeURIComponent(usrPassword)
    },
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});

但是当我收到它时,我在 node.js 中的 req 上的任何地方都找不到数据。我在这里做错了什么?

【问题讨论】:

  • 如果出现错误,请检查浏览器控制台
  • 我没有收到任何错误
  • req对象上根本找不到数据
  • 在客户端试试这个并检查是否真的被编码。 console.log(encodeURIComponent(usrPassword))
  • 切换到https协议

标签: angularjs node.js http-post


【解决方案1】:

你可以找到:

  • 在req.body 上发布数据
  • 在req.query 上获取参数
  • req.params 上的路由参数

req.body.userName 为您的示例

【讨论】:

  • req.body 对我来说是未定义的:/
  • 确保您的 bodyparser 配置正确/匹配您的内容类型标头。 reference
猜你喜欢
  • 2015-10-23
  • 1970-01-01
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
相关资源
最近更新 更多