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


出現這個,就是跨域問題了,我是在vue用axios直接發送請求向java后台的過程中出現的

至於什么是跨域問題?

主要是有瀏覽器的同源策略導致的,目的就是為了保護網站使用者,若沒有同源策略,那么在www.1.com的網站里就可以訪問www.baidu.com的內容的這樣就使www.1.com免費蹭了了baidu的服務,baidu肯定不願意的,瀏覽器為了維護各個網站的安全就有了同源策略

然后我是在后端中解決的代碼如下加上下面幾個代碼即可

@Configuration
public class WebConfig implements WebMvcConfigurer{


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 設置允許跨域的路徑
        registry.addMapping("/**")
            // 設置允許跨域請求的域名
            .allowedOrigins("*")
            // 是否允許證書
            .allowCredentials(true)
            .allowedMethods("*")
            .maxAge(3600);
    }

        ...
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 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. 接口在dev環境報跨域問題(has been blocked by CORS policy:Response to preflight request doesn't pass access control check:No 'Access-Control-Allow-Origin' header ispresent on the requested resource.),qa環境正常 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed a 跨域請求錯誤: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource 跨域詳解 been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 跨域詳解 been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 前端出現has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade Access to XMLHttpRequest at '*url*' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. GET *url* net::ERR_FAILED Access to XMLHttpRequest at 'http://localhost:8090/user/getotp' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM