我可以在我的节点控制台上以json格式console.log() API调用的响应,我如何让它显示结果到我的网页上,并带有样式。
基本上,在app.post()中,我在发出请求后执行了console.log()。我正在得到回复,但我需要在我的网页上显示它。
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;
})
});
希望这能有所帮助。
发布于 2020-05-16 16:41:09
在响应对象集类型中,JSON解析并发送。
res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({ a: 1 }));
https://stackoverflow.com/questions/61840164
复制相似问题