服务器端(app.js):
我从数据库获取一些数据并将其放入数组中
app.post('/sendform', (req,res) =>{
var array = [{"h":"1"},{"e","2"}];
res.send(array);
)}
客户端(client.js):
我想获取该数组并将其添加到我的index.html
function tableContent(){
fetch('/')
.then(res => {
console.log(res);
if(res.ok) return res.json();
});
.then(data => {
//do something with html file
});
}
最佳答案
var URL = “127.0.0.1:3000/sendform”;
var req = new Request(URL, {method: 'POST', cache: 'reload'});
fetch(req).then(function(response) {
var reader = response.body.getReader();
return reader.read();
}).then(function(result, done) {
if (!done) {
// do something with each chunk
}
});
POST 方法请求您的地址
关于javascript - 如何在客户端调用fetch方法从服务器端接收数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50032983/