【发布时间】:2019-11-23 07:56:14
【问题描述】:
拥有一个 json 对象,我使用 fetch API 进行了 API 调用,得到了预期结果的响应。如何将结果发送给客户端,以便能够在界面中显示它们?我使用了 Node.js 的 express 模块。
const fetch = require("node-fetch");
const express = require("express");
var bodyParser = require('body-parser');
const app = express();
app.post('/', (req, res) => {
res.sendStatus(200);
jsonBody = JSON.parse(fs.readFileSync("./resources/jsonBody.json", {
encoding: 'utf8'
}));
function results() {
fetch(url, {
method: 'POST',
body: JSON.stringify(jsonBody),
headers: {
"Authorization": "Bearer " + predictionKey,
"Content-Type": "application/json"
},
}).then(response => response.json()).then(response =>
console.log(response.Results.output1.value.Values));
}
results()
})
});
客户端发帖功能:
function postMessage(message){
$.post("http://localhost:8000",message);
}
这是服务端api调用的响应
[ [ '1008', '0', '1' ],
[ '1079', '1', '3' ],
[ '1022', '2', '3' ] ]
我想在界面中显示这些值(在客户端)
【问题讨论】:
-
你能添加更多关于客户的细节吗?它是什么类型的客户?在界面上显示值是什么意思?
标签: javascript node.js express client-server fetch-api