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



你可能感兴趣的:(Grails)