小编Dus*_*son的帖子

使用Express.js和NodeJS,您可以通过响应正文中的重定向发送JSON

我试图通过302重定向发送JSON.这在ExpressJS中是否可行.API声明可以添加正文res.json().例如:

res.json(302, {'name': 'larry'}).
Run Code Online (Sandbox Code Playgroud)

在接收端(重定向也在那里),身体是空的.以下是一些示例代码:

发送应用程序

app.get('/hello', function(req,res){
  var data = {'name': 'larry'};
  res.set('location', 'http://www.example.com/sending');
  res.json(302, data);
});
Run Code Online (Sandbox Code Playgroud)

接收应用程序

app.get('/sending', function(req,res){
  console.log('Body: ' + req.body)
  res.send(req.body);
});
Run Code Online (Sandbox Code Playgroud)

注意:302的响应标头显示数据的正确内容长度,但接收端显示空对象.

json node.js express

4
推荐指数
1
解决办法
9765
查看次数

标签 统计

express ×1

json ×1

node.js ×1