【发布时间】:2016-03-05 04:26:20
【问题描述】:
我在一个项目中使用 Angular 1.3 和 node.js 0.12.2。我正在使用
访问 node.js api$http.post("url", request_data){}
在服务器端使用这个:
console.log(req.body)
但是每次调用 api 时,它都会为 {} 获取空对象 request_data ,无法得到问题所在。我已经像这样使用body_parser:
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
还尝试在 angular $http 中添加 content-type 标头:
headers : {'Content-Type': 'applicatio n/x-www-form-urlencoded'}
但没有得到请求数据。
编辑:
Node.js 代码:
router.post('/url',function(req,res){
console.log(req.body)
})
注意:开发人员工具的网络选项卡显示数据,我正在发送,在请求标头中正确,但 node.js 服务器未在req.body 中接收。
在 POSTman 中获取数据是正确的响应。
【问题讨论】:
-
能不能分享一行nodejs代码
-
req.body 或 res.body
-
$http.post('url',{data:'data'}, function(res){console.log(res)})
-
尝试使用 postman 或 rest 客户端来测试你的 api
-
@JayantPatil -$http.post 是 Angular 向节点 api 发送数据的方法。而
console.log(res.body)在 node.js 服务器上用于获取请求数据 @server 端。我已经在邮递员中对其进行了测试,它显示了我的自定义错误,即没有收到请求数据,即使“开发者工具”的网络泛在请求标头中显示数据。
标签: angularjs json node.js http-post