How to enable CORS in Grails 3.0.1

后端 未结 7 1319
有刺的猬
有刺的猬 2021-02-18 22:45

I would like to do cross origin communication using Grails in server side. The only documentation that I found is this one

https://grails.org/plugin/cors

but thi

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-18 23:02

    To be specific, here is some code that works. Notice the interceptor name must match your controller name (here, workRequest), the domain needs to be whatever you are calling from (here, localhost:8081) and it is the before() method you want:

    package rest
    class WorkRequestInterceptor {
    boolean before() { 
        header( "Access-Control-Allow-Origin", "http://localhost:8081" )
        header( "Access-Control-Allow-Credentials", "true" )
        header( "Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE" )
        header( "Access-Control-Max-Age", "3600" )  
        true 
    }
    
    boolean after() { true }
    }
    

提交回复
热议问题