【问题标题】:Trying to send JSON response with Restify尝试使用 Restify 发送 JSON 响应
【发布时间】:2015-02-18 04:43:36
【问题描述】:

我正在尝试使用当前调用的 API 在 angel.co 上列出所有带有特定标签的工作

https://api.angel.co/1/tags/10/startups

然后尝试解析它并使用restify在浏览器中显示

var tagUrl = "https://api.angel.co/1/tags/10/startups"
    request({
        url: tagUrl,
        json: true
    }, function (error, response, body) {

        if (!error && response.statusCode === 200) {
           console.log(body) // Print the json response
        }
        else console.log("error" + error)
    })

我正在让 console.log(body)-part 工作,但是当我尝试将响应发送到浏览器时,当我尝试使用

发送它时它没有显示任何内容
res.send('hello ' + req.params.name + body);

我应该以某种方式对其进行解析或字符串化吗?

编辑:这是最终代码

function respond(req, res, next) {
var tag = req.params.tag;
var url = "http://api.angel.co/1/tags/"+tag+"/startups/?


request({
     url: url,
    json: true
}, function (error, response, body) {

if (!error && response.statusCode === 200) {
    console.log(body) // Print the json response
    res.send( req.params.name + JSON.stringify(body));

}
else console.log("error" + error)
})

【问题讨论】:

  • 不,我在服务器端使用 node.js,它获取并解析 URL。然后我使用 res.send 方法将其呈现给访问某个 URL 的用户

标签: javascript json node.js restify


【解决方案1】:

使用json的stringify属性,将json格式转换为字符串

JSON.stringify(body)

【讨论】:

    猜你喜欢
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2012-10-10
    • 2012-07-23
    相关资源
    最近更新 更多