【问题标题】:I am using heroku to test my application. how to solve cors problem?我正在使用 heroku 来测试我的应用程序。如何解决cors问题?
【发布时间】:2021-10-22 05:43:26
【问题描述】:

错误: 我正在使用 heroku 来测试我的应用程序。后端(nodejs-express)前端用react写

进入网站,接收数据时出错

如何处理

Access to fetch at 'my-web_api/allCity' from origin 'my-web_api' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

【问题讨论】:

    标签: node.js reactjs heroku cors


    【解决方案1】:

    您必须安装 cors(Cross-Origin Resource Sharing) 包以避免此类错误

    npm i cors
    

    并像这样在你的 server.js 文件中使用它

     const cors = require('cors')
     app.use(cors())
    

    【讨论】:

    • 请问,您还能如何解决这个问题?我不能再解决这个问题 5 个小时了。尝试在请求中设置 cors: 'no-cors',但仍然报错
    • @HLEBHLEB 看看这篇文章html5rocks.com/en/tutorials/cors这将解决问题。您需要添加 Access-Control-Allow-Origin。
    猜你喜欢
    • 2020-05-27
    • 2020-10-05
    • 2020-08-19
    • 2021-08-02
    • 2021-06-04
    • 2016-06-22
    • 1970-01-01
    • 2019-04-07
    • 2022-01-15
    相关资源
    最近更新 更多