【问题标题】:Sending multiple responses(res.json) with the same response object in Express.js在 Express.js 中使用相同的响应对象发送多个响应(res.json)
【发布时间】:2017-08-02 13:20:37
【问题描述】:
res.json(Object.assign({}, cart.generateArray()));
res.json(JSON.stringify(cart.totalPrice));

由于我的代码不起作用,我如何发送多个响应

谢谢

【问题讨论】:

  • 请提及您的代码,以便我们根据您的要求提供帮助

标签: node.js express


【解决方案1】:

您不能发送多个回复。您发送一个包含您的数组和总价的对象:

res.json({
    items: cart.generateArray(),
    totalPrice: cart.totalPrice
});

如果您需要两个响应,另一种选择是发出两个不同的请求。

【讨论】:

  • FWIW,我使用 Andy 的解释在 Express.JS 和 Node.JS 配置中执行 res.json({responseObjects: myArrayOfJSONObjects})。完美运行!
猜你喜欢
  • 1970-01-01
  • 2019-05-27
  • 2016-09-13
  • 1970-01-01
  • 2021-04-20
  • 2015-04-23
  • 2018-08-19
  • 1970-01-01
  • 2017-10-18
相关资源
最近更新 更多