首页 > 解决方案 > 在向 grails 发出请求时遇到 cors 问题

问题描述

我收到这个问题

从源“ http://localhost:4200 ”访问“ http://localhost:8082/v1/store/getstore ”的 XMLHttpRequest已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:重定向不允许用于预检请求。

我在 application.yaml 中的配置是

grails:
cors:
    enabled: true
    allowedOrigins:
    - http://localhost:4200

并在 url 映射中

"/v1/store/getStore"(controller: 'store', action: 'getStore')

当我直接以“ http://localhost:8082/store/getstore ”的形式点击请求时,它不会抛出错误,但在放入 urlMapping 后会抛出 cors 问题。

标签: javaangulargrailscors

解决方案


在根文件夹中创建 proxy.conf.json 文件并编辑以下内容:

{
    "/": {
      "target": "http://localhost:8082/",
      "secure": false
    }
  }

将 package json 命令更改为启动:

"start": "ng serve --proxy-config proxy.conf.json --open",

使用相同的 URL 映射"/v1/store/getStore"(controller: 'store', action: 'getStore')


推荐阅读