當出現403跨域錯誤的時候 No 'Access-Control-Allow-Origin' header is present on the requested resource,須要給Nginx服務器配置響應的header參數:
1、 解決方案
只須要在Nginx的配置文件中配置如下參數:json
location / { 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,Authorization'; if ($request_method = 'OPTIONS') { return 204; } }
上面配置代碼便可解決問題了,不想深刻研究的,看到這裏就能夠啦=-=
2、 解釋segmentfault
Access-Control-Allow-Origin *
後,表示服務器能夠接受全部的請求源(Origin),即接受全部跨域的請求。Access-Control-Allow-Methods 是爲了防止出現如下錯誤:
Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.跨域
PHP解決跨域問題,加入header頭:服務器
header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE"); header('Access-Control-Allow-Headers:x-requested-with,content-type');
原文地址:
http://www.javashuo.com/article/p-zlhfinyg-gq.html
https://blog.csdn.net/HQB421/article/details/80505073app