繁体   English   中英

如何将API响应作为JSON对象发送到客户端?

[英]How to send API response as JSON object to client?

响应是来自字典API的JSON对象。 响应成功登录到控制台。 如何将响应中包含的JSON对象发送回客户端?

router.get('/dictionary_test', (req, res, next) => {
  const lookup = dict.find("apple");
  lookup.then(res => {
    console.log(res);
  },
  (err) => {
    console.log(err);
  })
});

拥有数据后,只需使用res.json() 并确保不通过意外定义另一个同名的本地参数来隐藏父res (请注意对result的更改,以免与res冲突):

router.get('/dictionary_test', (req, res, next) => {
  const lookup = dict.find("apple");
  lookup.then(result => {
    console.log(result);
    res.json(result);
  }, err => {
    console.log(err);
    res.sendStatus(500);
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM