有什么办法可以解决json-server和angular 8中的cors问题?

问题描述

我想在Angular 8应用程序中使用JSON服务器模拟数据,不幸的是,当我使用http:// localhost:3000 / api / posts调用JSON服务器API时。它给出了以下错误

CORS策略已阻止从源“ http:// localhost:4200”访问“ http:// localhost:3000 / api / posts”处的XMLHttpRequest:对预检请求的响应未通过访问控制检查:请求的资源上没有“ Access-Control-Allow-Origin”标头。

我已经在我的应用程序中使用了proxy.conf.json的所有可能的解决方案,我也关注了许多文章和堆栈溢出线程,但是在我的情况下不起作用。

    "/api": {
      "target": "http://localhost:3000","secure": true,"pathRewrite": {
      "^/api": ""
    },"changeOrigin": true
    }
  }

另外,我已经在package.json中添加了它

npm运行环境-s && ng服务--proxy-config proxy.conf.json

如何解决Angular 8 CLI中的cors问题?

我遵循的文章参考。

  1. https://levelup.gitconnected.com/fixing-cors-errors-with-angular-cli-proxy-e5e0ef143f85

  2. https://www.c-sharpcorner.com/article/making-proxy-request-in-angular-fix-cors-issue-in-angular-application/

  3. CORS Angular 8 it gives me

  4. https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)