簡體   English   中英

如何解決 spring 啟動應用程序中的 CORS 問題?

[英]How to resolve CORS issue in spring boot application?

我嘗試使用 js 代碼來獲取遠程(spring boot) js 代碼,它顯示

Cross-Origin Read Blocking (CORB) blocked cross-origin response http://45.82.79.208/ with MIME type text/plain. See https://www.chromestatus.com/feature/5629709824032768 for more details

我的js代碼:

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://45.82.79.208/";
document.head.appendChild(script);

我的 spring 引導代碼,允許交叉:

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // allow cros path
        .allowedOrigins("*")    // allow cros origin
        .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")// allow method
        .maxAge(168000) //
        .allowedHeaders("*")
        .allowCredentials(true);
    }
}

我的代碼有問題嗎? 如何解決此問題以允許 CORS?

根據頁面,響應類型不正確。 可以嘗試設置js的響應header(content-type)

它將阻止從 or 標記請求的跨域文本/html 響應,將其替換為空響應

啟用 CORS 很簡單——只需將注釋 @CrossOrigin 添加到您的控制器。 請查看此網站了解更多詳情: https://www.baeldung.com/spring-cors

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM