前端跨域問(wèn)題解決方式:
1.與服務(wù)端部署到同域上(正常部署)
2.CORS
同域安全策略CORS(Cross-Origin Resource Sharing)
它要求請(qǐng)求的服務(wù)器在響應(yīng)的報(bào)頭(Response Header)添加 Access-Control-Allow-Origin標(biāo)簽,從而允許此標(biāo)簽所對(duì)應(yīng)域訪問(wèn)此服務(wù)器的資源,調(diào)用此服務(wù)器的接口。
缺陷:
默認(rèn)情況下,跨源請(qǐng)求不提供憑據(jù)(cookie、HTTP認(rèn)證及客戶端SSL證明等),通過(guò)將withCredentials屬性設(shè)置為true,可以指定某個(gè)請(qǐng)求應(yīng)該發(fā)送憑據(jù)。如果服務(wù)器接收帶憑據(jù)的請(qǐng)求,會(huì)用下面的HTTP頭部來(lái)響應(yīng):
Access-Control-Allow-Credentials: true
如果發(fā)送的是帶憑據(jù)的請(qǐng)求,但服務(wù)器的相應(yīng)中沒(méi)有包含這個(gè)頭部,那么瀏覽器就不會(huì)把相應(yīng)交給JavaScript,請(qǐng)求就無(wú)法得到結(jié)果的數(shù)據(jù)(瀏覽器得到了,但是我們請(qǐng)求的方法得不到,因?yàn)楸粸g覽器攔截了),因此在需要傳Cookie等時(shí),服務(wù)端的Access-Control-Allow-Origin必須配置具體的具體的域名。并且還需要設(shè)置其他的請(qǐng)求頭:
Access-Control-Allow-Origin; //request.getHeader("Origin")||www.xxx.com
Access-Control-Allow-Methods; //POST, GET, OPTIONS, DELETE
Access-Control-Allow-Credentials;//是否支持cookie跨域
Access-Control-Allow-Headers;//x-requested-with,content-type
請(qǐng)求頭
- Access-Control-Request-Method : 先導(dǎo)請(qǐng)求中的請(qǐng)求頭,告訴服務(wù)器真實(shí)請(qǐng)求的http方法
- Access-Control-Request-Headers :先導(dǎo)請(qǐng)求中的請(qǐng)求頭,告訴服務(wù)器真實(shí)請(qǐng)求的http請(qǐng)求頭
響應(yīng)頭
- Access-Control-Allow-Origin :服務(wù)器允許跨域請(qǐng)求的origin
- Access-Control-Expose-Headers : 允許JavaScript讀取的頭部
- Access-Control-Allow-Credentials :是否允許攜帶cookie
- Access-Control-Allow-Methods :允許的請(qǐng)求方法
- Access-Control-Allow-Headers :允許的請(qǐng)求頭部
配置
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
}
3. jsonp方式
4.代理服務(wù)器方式
自己寫一個(gè)php,或者Java的服務(wù)端 將web項(xiàng)目與此項(xiàng)目發(fā)布到服務(wù)器,這樣web項(xiàng)目就能直接調(diào)用此服務(wù)器的內(nèi)容,然后再用此服務(wù)器調(diào)用目標(biāo)服務(wù)器的內(nèi)容,因?yàn)楸荛_了瀏覽器的限制,因此這種方式也是能成的,但是需要注意,這種情況下因?yàn)镃ookie有path和 domain的限制,因此需要截取目標(biāo)服務(wù)器的Cookie,將其改為代理服務(wù)器的path和domain,否則在需要使用到Cookie的時(shí)候,由于path或domain不對(duì),不能將Cookie轉(zhuǎn)發(fā)到目標(biāo)服務(wù)器。
server {
listen 80;
server_name localhost;
location / {
root /Users/Shadow/Sites;
}
error_page 500 502 503 504 /50x.html;
#relative path
location = /50x.html {
root html;
}
#absolute path
location /douban/{
proxy_pass http://www.itdecent.cn/;
proxy_cookie_path / /jianshu;
}
}
那么前端ajax時(shí),訪問(wèn)的域名地址是本服務(wù)器的地址,比如:
http://localhost/jianshu/index.php?r=test
它包含“jianshu”,因而會(huì)跳轉(zhuǎn)到:
https://m.jianshu.com/index.php?r=test