最近在了解GraphQL,根据https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/里面的例子搭建了一个demo,结果在使用GraphQL playground来查看GraphQL demo结果的时候,报错:
Failed to fetch. Please check your connection
F12查看前台请求,发现报错为:
Access to fetch at ‘http://localhost:8080/graphql’ from origin
‘https://www.graphqlbin.com’ has been blocked by CORS policy: No
‘Access-Control-Allow-Origin’ header is present on the requested
resource. If an opaque response serves your needs, set the request’s
mode to ‘no-cors’ to fetch the resource with CORS disabled.
所以原因是跨域问题。
Spring MVC配置CORS的方式,搜索了一下找到一个博客
Spring MVC配置CORS(解决跨域请求)
采用其中的添加全局配置的方式,解决了问题。
GraphQL playground可以调用到后台的demo了!