java– 带有d簧启动的’Access-Control-Allow-Origin’

java– 带有d簧启动的’Access-Control-Allow-Origin’,第1张

概述我有一个简单的spring启动服务,它运行在端口8080上暴露的docker容器中,该容器正在调用mysql数据库.当我点击localhost:8080 / blogs时,我回来了[{“作者”:“Christopher Bolton”,“标题”:“测试标题1”,“内容”:“这是一些内容”,“日期”:“2017 -08-29\' }]当我直接在浏览器中点击它时

我有一个简单的spring启动服务,它运行在端口8080上暴露的docker容器中,该容器正在调用mysql数据库.

当我点击localhost:8080 / blogs时,我回来了[{“作者”:“Christopher Bolton”,“标题”:“测试标题1”,“内容”:“这是一些内容”,“日期”:“2017 -08-29\” }]

当我直接在浏览器中点击它时,这工作得很好.但是,当我从jquery尝试它时,我得到了正常的Access-Control-Allow-Origin.

这是我的春季启动服务:

@SpringBootApplication@RestControllerpublic class ChrisboltonServiceApplication {public static voID main(String[] args) {    SpringApplication.run(ChrisboltonServiceApplication.class,args);}@autowiredprivate JdbcTemplate jdbcTemplate;@CrossOrigin@RequestMapPing(path="/blogs")public @ResponseBody Iterable

这是我的jquery:

$.AJAX({  url: "http://localhost:8080/blogs",crossDomain: true}).done(function(data) {  console.log(data);});

但我仍然收到此错误:

XMLhttpRequest cannot load http://localhost:8080/blogs. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

我已经通过将@CrossOrigin添加到getAllUsers()方法尝试了this,并且我在类级别尝试过.我也看了this,因为我在端口3000上运行我的UI.但是这个链接不是特定于Spring的.

编辑

添加我的请求标头:

GET /blogs http/1.1Host: localhost:8080Connection: keep-aliveAccept: */*Origin: http://localhost:3000User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/60.0.3112.113 Safari/537.36Referer: http://localhost:3000/Accept-EnCoding: gzip,deflate,brAccept-Language: en-US,en;q=0.8

网络标签中的响应(Chrome):

[{“author”:“Christopher Bolton”,“Title”:“Test Title 1”,“content”:“这是一些内容”,“date”:“2017-08-29”}]

所以看起来我正在网络选项卡中获取数据.但是,我的console.log(data)生成了Access-Control-Allow-Origin

最佳答案尝试将此添加到您的应用程序:

@SpringBootApplication@RestControllerpublic class ChrisboltonServiceApplication {    @Bean    public WebMvcConfigurer corsConfigurer() {        return new WebMvcConfigurerAdapter() {            @OverrIDe            public voID addCorsMapPings(CorsRegistry registry) {                registry.addMapPing("/**").allowedOrigins("*");            }        };    }...

另外,尝试从$.AJAX()中删除crossDomain:true. 总结

以上是内存溢出为你收集整理的java – 带有d簧启动的’Access-Control-Allow-Origin’全部内容,希望文章能够帮你解决java – 带有d簧启动的’Access-Control-Allow-Origin’所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1265847.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)