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

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

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

问题

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

> CORS策略问题

代码如下:

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

错误信息:

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

Spring Boot Java服务的主要函数:

@Bean
public WebMvcConfigurer configure() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry corsRegistry) {
            corsRegistry.addMapping("/**").allowedOrigins("http://localhost:8080/");
        }
    };
}
英文:

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:

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

Error:

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

Spring boot java service main function:

@Bean
	public WebMvcConfigurer configure() {
		return new WebMvcConfigurer() {
			@Override
			public void addCorsMappings(CorsRegistry corsRegistry) {
				corsRegistry.addMapping("/**").allowedOrigins("http://localhost:8080/");
			}
		};
	}

答案1

得分: 0

Sure, here's the translated code part:

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

@CrossOrigin(origins = "*")

或者

@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

@CrossOrigin(origins = "*")

or

@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:

确定