【问题标题】:Angular: How to set proxy to avoid CORS errorAngular:如何设置代理以避免 CORS 错误
【发布时间】:2019-06-25 17:05:08
【问题描述】:

在 Angular 应用程序中,我以这种方式拥有 proxy.conf.json

{
  "/api/*": {
    "target": "http://10.127.152.161:8080",
    "secure": true,
    "changeOrigin": true
  },

  "/rest/*": {
    "target": "http://10.127.152.161:8080",
    "secure": true,
    "changeOrigin": true
  },

  "/auth/*": {
    "target": "http://10.127.152.161:8080",
    "secure": true,
    "changeOrigin": true
  }
}

由于在开发模式下,Angular 应用程序在 localhost:4200 上运行,将目标设置为 ipv4 地址:8080 会导致 CORS 标头问题。由于某些原因,我必须使用 http://10.127.152.161:8080 不是 localhost:8080 或 3000 端口。我添加了 add_header 'Access-Control-Allow-Origin' '*'; nginx conf 文件,但没有运气。如何解决这个问题?

【问题讨论】:

    标签: angular nginx cors


    【解决方案1】:

    "secure" 属性设置为false

    {
      "/api/*": {
        "target": "http://10.127.152.161:8080",
        "secure": false
    },
    ...
    

    【讨论】:

    • httpClient.get('10.127.152.161:8080/api/foo'), httpClient.get('/api/foo') 你的情况是?
    • 它是 httpClient.get('/api/foo')
    猜你喜欢
    • 2021-08-31
    • 2021-05-09
    • 2023-03-18
    • 2022-01-25
    • 1970-01-01
    • 2017-12-16
    • 2022-12-05
    • 2012-12-08
    • 2018-03-31
    相关资源
    最近更新 更多