如何在Grails Rest API中使用CORS? [英] How to use CORS with grails rest API?

查看:80
本文介绍了如何在Grails Rest API中使用CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用grails开发Rest Api.它正在localhost:8080上运行.当我用POSTMAN打电话时,它以json响应.我想使用此数据,并且还要在其他域上运行的网页上执行CRUD操作.当我尝试调用用grails编写的RESTAPI时,由于Access-Control-Allow-Origin,浏览器无法获得响应.那么如何在我的REST API中启用CORS功能,则任何人都可以从其他域进行访问.

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.

推荐答案

如果您使用的是Grails 3.2.1或更高版本,则它内置了CORS支持.

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

在您的application.yml中,添加以下内容:

In your application.yml, add the following:

grails:
    cors:
        enabled: true

请参见 http://docs.grails.org/latest/guide/Introduction.html#whatsNewCors 以获得更多详细信息.

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

这篇关于如何在Grails Rest API中使用CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆