**
一、问题
出现了跨域无法访问的问题
**
二、代码
1、客户端:
fetch('http://localhost:3000/books', {
method: 'post',
body: JSON.stringify({
uname: '张三',
pwd: '456'
}),
headers: {
'Content-Type': 'application/json'
}
}).then(function(data) {
return data.text();
}).then(function(data) {
console.log(data)
});
2、服务端允许跨域访问的一些设置
// 设置允许跨域访问该服务
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Headers', 'mytoken');
next();
});
三、解决方法
在服务端的允许跨域访问中加入以下代码
res.header('Access-Control-Allow-Headers', '*');
成功解决: