The 'Access-Control-Allow-Origin' header contains multiple values'*, *', but only one is allowed.


轉自 https://blog.csdn.net/xxwd12/article/details/98882461

1、Failed to load http://xx.com/mobile/service: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'http://localhost:8081' is therefore not allowed access.
 

解決:

出現這問題有可能是服務器端配置了跨域選項,在代碼端也指定了跨域設置導致沖突,去掉一邊的配置即可

代碼中配置了跨域攔截器:

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials","true");
        return super.preHandle(request, response, handler);
    }

nginx配置了:

  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;    
  add_header Access-Control-Allow-Headers X-Requested-With;

 去掉其中之一。

2、Failed to load http://xx.com/mobile/service: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access.
 這是跨域錯誤

解決:

上面兩種方式選其一。

第三中方式:使用@CrossOrigin注解進行細粒度處理,@CrossOrigin可以使用在類和方法上面

其中@CrossOrigin中的2個參數:

origins  : 允許可訪問的域列表

maxAge:准備響應前的緩存持續的最大時間(以秒為單位)。

 


免責聲明!

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



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM