【发布时间】:2018-07-08 20:56:25
【问题描述】:
我是 Web 开发的新手,我正在尝试将一些 JSON 数据发送到运行 express 的 node.js 服务器,但我收到了这个错误:
无法加载http://localhost:8888/:方法 PUT 不允许 预检响应中的 Access-Control-Allow-Methods。
我不知道这意味着什么。这是客户端获取:
fetch('http://localhost:8888/', {
method: 'PUT',
body: JSON.stringify(this.exercises),
headers: {
'Content-Type': 'application/json'
}
}).then(res => res.json())
.catch(err => console.error('Error: ' + err))
.then(res => console.log('Success: ' + res));
这是服务器端代码:
app.put('/', (req, res, next) => {
console.log('PUT request received');
console.log(req.body);
});
服务器似乎甚至没有收到请求。我该如何进行这项工作?提前致谢。
【问题讨论】:
-
您的服务器没有响应任何数据。所以你的客户会一直挂在那里直到超时。尝试在最后一个 console.log 之前添加
res.send('');。
标签: javascript node.js express fetch