gpt4 book ai didi

spring - CORS 策略已阻止从源访问 XMLHttpRequest

转载 作者:搜寻专家 更新时间:2023-10-30 22:50:15 25 4
gpt4 key购买 nike

我在使用 VueJS 和 Spring 时遇到 CORS 错误。 VueJS 使用 POST 调用 REST WS。我在下面尝试通过创建 CorsConfiguration 并添加来源、 header 和方法来允许 CORS,但它没有用。感谢您的帮助。

浏览器

OPTIONS https://test/example/ 403

Access to XMLHttpRequest at 'https://test/example/' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Spring

@Configuration
public class CustomCORSConfiguration {

private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}

@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}

视觉

//POST
const config = {
headers: {
'Content-Type': 'application/json'
},
strictSSL: false,
changeOrigin:true,
rejectUnauthorized: true,//add when working with https sites
requestCert: false,//add when working with https sites
agent: false//add when working with https sites
}
const reqBody = {
"shopCode": "ASCDASDCASD",
"amount": this.total_price,
"authToken": "EDSAACDSAD"
}

this.axios.post(api, reqBody, config).then(response => {
if (response) {
const data = response.data
console.log(data)
this.qrUrl = data.data
console.log(this.qrUrl)
//if (this.qrUrl) {
// this.loading=false
location.href = this.qrUrl
//}
}
}).catch(e => {
this.errors=e
alert(e)
console.log(this.errors)
})

最佳答案

你能试试加行吗

corsConfiguration.setAllowCredentials(true);

因此你的代码看起来像

CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("*");

关于spring - CORS 策略已阻止从源访问 XMLHttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53496336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com