nginx 配置add_header 'Access-Control-Allow-Origin' '*' 依然存在跨域問題


.問題描述:
前端域名A 在POST請求后端域名為B 的一個接口時候請求成功時不存在跨域問題,請求失敗時瀏覽器提示跨域。

解決:
當請求成功時,HTTP CODE 為200。而請求失敗時HTTP CODE 為400, 此時add_header ‘Access-Control-Allow-Origin’ ‘*’ 配置無效!設置無論HTTP CODE 為何值時都生效需要加 always 。nginx版本>1.7.5時候無須加always。

add_header 'Access-Control-Allow-Origin' '*' always;

nginx文檔地址:

http://nginx.org/en/docs/http/ngx_http_headers_module.html

文檔原文:

Syntax: add_header name value [always];
Default: —
Context: http, server, location, if in location

If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.]

 


免責聲明!

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



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