【问题标题】:Strapi GraphQL API - 405 Method Not AllowedStrapi GraphQL API - 405 方法不允许
【发布时间】:2021-02-25 20:36:22
【问题描述】:

我刚刚按照文档将我的 Strapi API 部署到生产环境:https://strapi.io/documentation/v3.x/deployment/heroku.html

通过从其他来源发出 POST 请求,我收到此错误:

由于我的 API 最终在开发中工作(本地主机到本地主机),我认为问题与 CORS 有关,如果是,那就是我在 API 上设置的 cors 配置:

config/env/production/security.json:

{
  "cors": {
    "enabled": true,
    "origin": "*"
  }
}

config/env/production/middleware.js:

module.exports = {
  settings: {
    cors: {
      enabled: true,
      origin: "*",
    },
  },
};

【问题讨论】:

  • 浏览器在 devtools 控制台中记录的确切错误消息是什么?
  • 附带说明:每个人都应该首先探索的是在您的项目上运行npm run dev。修改模型和路线后,这清除了我使用 Strapi 的 405 错误。

标签: api heroku graphql cors strapi


【解决方案1】:

在我的例子中,由于我使用 GraphQL 进行查询,因此我应该在生产 URL 的末尾添加 /graphql,就像我们为 localhost:1337/graphql 所做的那样。

【讨论】:

    【解决方案2】:

    对我来说,api/blog/config/routes.json 中缺少路线

    {
      "method": "POST",
      "path": "/blog",
      "handler": "blog.create",
      "config": {
        "policies": []
      }
    },
    

    【讨论】:

      【解决方案3】:

      我在开发环境中添加了 cors 配置...

      为了改变生产环境的cors配置,上面的代码应该放在以下路径:

      config/env/production/security.json 
      config/env/production/middleware.js
      

      【讨论】:

        猜你喜欢
        • 2013-03-21
        • 2016-01-23
        • 1970-01-01
        • 2018-02-04
        • 2016-03-23
        • 1970-01-01
        • 1970-01-01
        • 2018-05-27
        • 2015-02-10
        相关资源
        最近更新 更多