How to use CORS with grails rest API?

2019-02-27 03:12发布

问题:

I am developing Rest Api using grails. It is working on localhost:8080. when I am calling with POSTMAN it responded with json. I want to use this data and also perform CRUD operation on my web page which is running on other domain. When I am trying to call RESTAPI which is written in grails, the browser is not able to get a response due to Access-Control-Allow-Origin. So how to enable CORS functionality in my REST API, So anyone can access from a different domain.

回答1:

If you are using Grails 3.2.1 or higher, it comes with CORS support built in.

In your application.yml, add the following:

grails:
    cors:
        enabled: true

See http://docs.grails.org/latest/guide/introduction.html#whatsNewCors for further details.