Vercel(Zeit)托管服务-CORS阻止REST请求

情况

我用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阻止了该应用程序的启动。

我已尝试解决的问题:

  1. 我联系了支持部门。对于他们而言,这并不是问题,因为看起来他们支持CORS。

    Vercel(Zeit)托管服务-CORS阻止REST请求

  2. 正在阅读相关文章,Zeit (Vercel) Now serverless authenticated requests failing because of CORS 。建议添加 now.json 。我认为这不适用于我的情况,因为它使用了Vercel的无服务器功能。

yhb200906 回答:Vercel(Zeit)托管服务-CORS阻止REST请求

哦,您必须安装并使用一个软件包:https://expressjs.com/en/resources/middleware/cors.html 遵循本指南,您将一路顺风。

本文链接:https://www.f2er.com/2315408.html

大家都在问