java - 在向 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 问题。
解决方案
在根文件夹中创建 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')
推荐阅读
- wcf - VS2017 中的 WCF 服务错误 400 错误请求
- javascript - 如何在 Pinterest、Twitter 和 facebook Javascript 文件中启用 gzip 压缩并利用浏览器缓存。
- kotlin - 如果最后一个操作以异常结束,Kotlin 协程立即给出异常
- ajax - 使用随机会话号保护 web api
- ubuntu-16.04 - Skylake 硬件亮度变化过于精细
- mysql - MySql 下拉过滤器选择 NULL
- git - Merge a remote branch on Heroku to Heroku Master to trigger a build
- javascript - Javascript - 跳过语句
- c# - 一旦发生重新调整大小,C# WPF 就会删除画布宽度和高度
- java - 无法在spring mvc中使用get请求调用控制器方法