赞
踩
Access to fetch at ‘http://localhost:3000/books/231’ from origin ‘http://127.0.0.1:5500’ has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
// 设置允许跨域访问该服务
app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Credentials', 'true');
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();
});
在网上找了挺多最后解决方法是
加了一句
用于判断request来自ajax还是传统请求
res.header("Access-Control-Allow-Headers", " Origin, X-Requested-With, Content-Type, Accept");
解决发送post请求时跨域无法访问
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。