情况
我用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的无服务器功能。
答案 0 :(得分:0)
哦,您必须安装并使用一个软件包:https://expressjs.com/en/resources/middleware/cors.html 遵循本指南,您将一路顺风。