【问题标题】:localhost and localhost:4200 CORS issue in angularlocalhost 和 localhost:4200 角度的 CORS 问题
【发布时间】:2026-01-29 16:45:01
【问题描述】:

在使用 ng serve

  1. 实现prox.conf.json然后运行

ng serve --proxy config proxy.conf.json

{
  "/`OSTNextService`": {

    "target": "https://localhost/OSTNextService",
    "secure": true,
    "pathRewrite": {
    "^/OSTNextService": ""
  },


"`changeOrigin": true`
  }
}
  1. 将 IIS 标头设置为指定域。在我的情况下 https://localhost:4200

  2. 设置服务 global.asax 文件

没有什么对我有用,并且出现像

这样的错误

访问 XMLHttpRequest 在 'https://localhost/OSTNextService/OSTPortalService.svc/GetUserName' 来自原点“https://localhost:4200”已被 CORS 策略阻止: 响应中“Access-Control-Allow-Origin”标头的值 当请求的凭证模式为 '包括'。发起请求的凭证模式 XMLHttpRequest 由 withCredentials 属性控制。

当我在 IIS 中托管应用程序并将 dist 文件夹映射到它时,一切正常。但是每次我更改构建应用程序所需的 .ts 文件几乎都是双倍的浪费。请通过建议我在这里缺少什么来提供帮助。

【问题讨论】:

  • 您的本地主机是否在安全协议上运行?

标签: angular api iis cors localhost


【解决方案1】:

在代理文件中使用“/OSTNextService”而不是“/OSTNextService”。如果您只使用"target": "https://localhost/",则不需要rewrite

也不要在你的应用程序中使用完整的 url,而是一个相对的/OSTNextService/OSTPortalService.svc/GetUserName,然后它会点击你的代理并转发到 localhost:80/...

我认为没有必要修改 IIS 标头。

【讨论】: