XmlHttpRequest请求中未提供’Access-Control-Allow-Origin’标头 – Chrome浏览器

huangapple go评论106阅读模式
英文:

XmlHttpRequest No 'Access-Control-Allow-Origin' header is present on the requested resource - Chrome browser

问题

我已经创建了一个Java服务来返回一个JSON对象。在Postman中它运行正常,但是当我尝试在Chrome浏览器中使用它时,我收到以下错误信息:

> CORS策略问题

代码如下:

  1. var xhttp = new XMLHttpRequest();
  2. var url = 'http://localhost:8030/information/agent111';
  3. xhttp.onreadystatechange = function() {
  4. if (this.readyState == 4 && this.status == 200) {
  5. console.log(this)
  6. }
  7. };
  8. xhttp.open("GET", url, true);
  9. xhttp.setRequestHeader('Content-Type', 'application/json' );
  10. xhttp.setRequestHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
  11. xhttp.setRequestHeader('Access-Control-Allow-Headers', 'http://localhost:8080');
  12. xhttp.setRequestHeader('Access-Control-Allow-Methods', 'GET');
  13. xhttp.setRequestHeader('Access-Control-Allow-Credentials', false);
  14. xhttp.send();

错误信息:

XmlHttpRequest请求中未提供’Access-Control-Allow-Origin’标头 – Chrome浏览器

Spring Boot Java服务的主要函数:

  1. @Bean
  2. public WebMvcConfigurer configure() {
  3. return new WebMvcConfigurer() {
  4. @Override
  5. public void addCorsMappings(CorsRegistry corsRegistry) {
  6. corsRegistry.addMapping("/**").allowedOrigins("http://localhost:8080/");
  7. }
  8. };
  9. }
英文:

I have created a java service to return a json object. It is working fine in postman but when i try it using the chrome browser i am getting below error

> CORS policy issue

the code:

  1. var xhttp = new XMLHttpRequest();
  2. var url = 'http://localhost:8030/information/agent111';
  3. xhttp.onreadystatechange = function() {
  4. if (this.readyState == 4 && this.status == 200) {
  5. console.log(this)
  6. }
  7. };
  8. xhttp.open("GET", url, true);
  9. xhttp.setRequestHeader('Content-Type', 'application/json' );
  10. xhttp.setRequestHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
  11. xhttp.setRequestHeader('Access-Control-Allow-Headers', 'http://localhost:8080');
  12. xhttp.setRequestHeader('Access-Control-Allow-Methods', 'GET');
  13. xhttp.setRequestHeader('Access-Control-Allow-Credentials', false);
  14. xhttp.send();

Error:

XmlHttpRequest请求中未提供’Access-Control-Allow-Origin’标头 – Chrome浏览器

Spring boot java service main function:

  1. @Bean
  2. public WebMvcConfigurer configure() {
  3. return new WebMvcConfigurer() {
  4. @Override
  5. public void addCorsMappings(CorsRegistry corsRegistry) {
  6. corsRegistry.addMapping("/**").allowedOrigins("http://localhost:8080/");
  7. }
  8. };
  9. }

答案1

得分: 0

Sure, here's the translated code part:

你需要让你的控制器知道请求的来源。
要解决跨域问题,请在你的控制器中添加以下代码。
你可以尝试使用通配符,像这样:

  1. @CrossOrigin(origins = "*")

或者

  1. @CrossOrigin(origins = "http://localhost:8080", allowCredentials = "true")
英文:

You need allow your controller to know about the origin of the request.
To Fix cors issue add below code to your controller.
You can try with wild card like

  1. @CrossOrigin(origins = "*")

or

  1. @CrossOrigin(origins = "http://localhost:8080" , allowCredentials = "true")

huangapple
  • 本文由 发表于 2020年7月28日 00:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63119417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定