前端vue2,后臺是springboot2.7.15
問題描述:點擊登錄的時候報錯:
Access to XMLHttpRequest at 'http://localhost:9091/login' (redirected from 'http://localhost:9090/dev-api/system/login/userLogin') from origin 'http://localhost:9090' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
解決方案:后臺編寫配置文件文章來源:http://www.zghlxwxcb.cn/news/detail-754775.html
package com.lucky.common.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Author lucky
* @Date 2023/10/23 10:57
* @PackageName:com.lucky.common.config
* @ClassName: CORSConfig
* @Description: //TODO
*/
@Configuration
public class CORSConfig implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry) {
// 設置允許跨域的路徑
registry.addMapping("/**")
// 設置允許跨域請求的域名
.allowedOriginPatterns("*")
// 是否允許cookie
.allowCredentials(true)
// 設置允許的請求方式
.allowedMethods("GET", "POST", "DELETE", "PUT")
// 設置允許的header屬性
.allowedHeaders("*")
// 跨域允許時間
.maxAge(10000);
}
}
最后:
網上百度了很多方法,有的網友在博主的帖子下面評論并沒有解決項目中遇到的跨域問題,我這邊博客也僅僅是記錄我項目遇到的跨域是怎樣解決的,能幫到遇到這種問題的小伙伴自然是再好不過了,不保證每個遇到這種問題都能按照我的方式去解決問題哈。有其他方式的小伙伴歡迎分享出來幫助更多的小伙伴。有問題可加876942434文章來源地址http://www.zghlxwxcb.cn/news/detail-754775.html
到了這里,關于解決Access to XMLHttpRequest at ‘http://localhost:9091/login‘ (redirected from ‘http://localhost:9090/的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!