涉及到多重跨域的話,項目訪問會出現無法跨域的報錯
即Java代碼部分,需要進行注釋掉:
//@CrossOrigin(allowCredentials = "true")
和下面的nginx設置同時允許開啟了跨域功能
配置示例:
add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,access-control-allow-origin,Authorization';
Access to XMLHttpRequest at 'https://manhua.wjoyxt.com/upload' from origin 'http://10.35.33.66:8083' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://10.35.33.66:8083, *', but only one is allowed.
程序內部如果已開啟允許跨域,那么Nginx再次開啟允許跨域的話,就會出現以上錯誤提示,所以兩者只能only one開啟
識別服務器允許的請求方法:
要找出服務器支持的請求方法,可以使用 curl 並發出 OPTIONS 請求:
curl -X OPTIONS http://example.org -i
該響應包含一個包含允許的方法的Allow
頭文件:
HTTP/1.1 200 OK
Allow: OPTIONS, GET, HEAD, POST
Cache-Control: max-age=604800Date: Thu, 13 Oct 2016 11:45:00 GMT
Expires: Thu, 20 Oct 2016 11:45:00 GMT
Server: EOS (lax004/2813)x-ec-custom-error: 1Content-Length: 0
跨域是前端開發中經常會遇到的問題,前端調用后台服務時,通常會遇到 No 'Access-Control-Allow-Origin' header is present on the requested resource的錯誤,這是因為
瀏覽器的同源策略拒絕了我們的請求。
所謂同源是指,域名,協議,端口相同,瀏覽器執行一個腳本時同源的腳本才會被執行。如果非同源,那么在請求數據時,瀏覽器會在控制台中報一個異常,提示拒絕訪問。
所謂同源是指,域名,協議,端口相同,瀏覽器執行一個腳本時同源的腳本才會被執行。如果非同源,那么在請求數據時,瀏覽器會在控制台中報一個異常,提示拒絕訪問。
跨域是指a頁面想獲取b頁面資源,如果a、b頁面的協議、域名、端口、子域名不同,所進行的訪問行動都是跨域的,而瀏覽器為了安全問題一般都限制了跨域訪問,也就是不允許跨域請求資源。
注意:跨域限制訪問,其實是瀏覽器的限制。
# vim nginx.conf
http { ###start### add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; ###end ### }
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Access-Control-Allow-Origin,Authorization';