情况
我用Vercel The database is in db.json托管了JSON数据库。我有一个要在数据库中创建和删除对象的站点。我已经配置了Axios,以便它可以允许PUT,PATCH,DELETE请求。
const streams = axios.create({
// .. where we make our configurations
baseURL: "https://music-json-server.now.sh/db.json",headers: {
"access-control-allow-origin": "*","access-Control-Request-Method": "*","access-Control-Allow-Credentials": "true","access-Control-Allow-Methods": "GET,HEAD,OPTIONS,POST,PUT","access-Control-Allow-Headers":
"Origin,X-Requested-With,Content-Type,accept,Authorization","Content-Type": "application/json",},});
问题 启动应用程序时,我遇到与CORS / axios配置相关的错误。
xhr.js:126 Refused to set unsafe header "access-Control-Request-Method"
access to XMLHttpRequest at 'https://music-json-server.now.sh/db.json/' from origin 'http://localhost:3001' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by access-Control-Allow-Headers in preflight response.
CORS阻止了该应用程序的启动。
我已尝试解决的问题:
-
正在阅读相关文章,Zeit (Vercel) Now serverless authenticated requests failing because of CORS 。建议添加 now.json 。我认为这不适用于我的情况,因为它使用了Vercel的无服务器功能。