No 'Access-Control-Allow-Origin' header is present on the requested resource.

前端之家收集整理的这篇文章主要介绍了No 'Access-Control-Allow-Origin' header is present on the requested resource.前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的环境:
| Grails Version: 3.1.4
| Groovy Version: 2.4.6
| JVM Version: 1.7.0_80

-------------------------------------------------------------------

错误信息:

XMLHttpRequest cannot load http://xxx. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://xxx:8100' is therefore not allowed access.

解决方法
1、对控制器进行拦截,然后在响应头中加入Access-Control-Allow-Origin","*"即可


如控制器是:UserController
则对应的拦截器是:UserInterceptor
class UserInterceptor {


boolean before() {
header("Access-Control-Allow-Origin","*") // 在这里加即可
true
}


boolean after() { true }


void afterView() {
// no-op
}
}
-----------------------------------------------------------------------------------------------------
注:grails创建拦截器命令如下:
grails create-interceptor 拦截名称
grails create-interceptor Book

grails create-interceptor org.bookstore.book

原文链接:https://www.f2er.com/ajax/162203.html

猜你在找的Ajax相关文章