首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在网页上显示API响应

在网页上显示API响应
EN

Stack Overflow用户
提问于 2020-05-17 00:37:20
回答 1查看 129关注 0票数 0

我可以在我的节点控制台上以json格式console.log() API调用的响应,我如何让它显示结果到我的网页上,并带有样式。

基本上,在app.post()中,我在发出请求后执行了console.log()。我正在得到回复,但我需要在我的网页上显示它。

代码语言:javascript
运行
复制
app.post("/",function(req,res){

            // INSTANTIATE A CLIENT
    let credentials= new CognitiveServicesCredentials('API-key-goes-here');
    let client=new WebSearchAPIClient(credentials);

            // MAKE REQUEST AND PRINT RESULT
    const query=req.body.item;
    client.web.search(query).then((result) => {
        let properties = ["images", "webPages", "news", "videos"];
        for (let i = 0; i < properties.length; i++) {
            if (result[properties[i]]) {
                // console.log(result);    // To see result types and all.
                // console.log(result[properties[i]]); // To see all tyes of results.
                // console.log(result.videos); // To display only the video results. 
                // res.send(result[properties[i]].value);
                // let url=(result[properties[i]].value);
                // console.log(url); // gives the same result.  

            } else {
                console.log(`No ${properties[i]} data`);
            }
        }
    }).catch((err) => {
        throw err;
    })
});

希望这能有所帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-17 00:41:09

在响应对象集类型中,JSON解析并发送。

res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({ a: 1 }));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61840164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档