【发布时间】:2018-04-24 12:25:36
【问题描述】:
我想使用 express 向本地 JSON 文件发出 GET 请求。
在我的 server.js 中有这个
var data = {};
app.get('/src/assets/data.json', (req, res) => {
console.log(res)
res.writeHead(200, {
'Content-type': 'application/json'
});
res.end(JSON.stringify(data));
});
data.json 看起来像这样
[{
"param": "one",
"param": "two",
"param": "three"
}]
我还为 GET 请求创建了一个函数,该函数在 DOM 加载后立即调用
getData() {
let xhr = new XMLHttpRequest();
xhr.open('GET', '/src/assets/data.json', true);
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
console.log(xhr)
}
};
xhr.send();
}
我收到了回复,但它是一个空对象。我猜这是因为在我的服务器文件中var data = {}; 是空的,但我不确定如何处理它?
【问题讨论】: