鍍金池/ 問(wèn)答/PHP  HTML/ https協(xié)議下,post請(qǐng)求跨域,get請(qǐng)求沒(méi)跨域;

https協(xié)議下,post請(qǐng)求跨域,get請(qǐng)求沒(méi)跨域;

https協(xié)議下,post請(qǐng)求跨域,get請(qǐng)求沒(méi)跨域,http協(xié)議沒(méi)問(wèn)題;

相關(guān)代碼

clipboard.png

clipboard.png

clipboard.png

clipboard.png

clipboard.png

回答
編輯回答
舊酒館

// 使用通配符 * ,表示當(dāng)前服務(wù)端 返回的信息允許所有源訪問(wèn),也可指定可信任的域名來(lái)接收響應(yīng)信息?

header("Access-Control-Allow-Origin: http://localhost:3000");

header("Access-control-Allow-Origin:*");

// 響應(yīng)頭設(shè)置為ajax提交?

header("Access-Control-Allow-Headers:X-Requested-With");

// 允許攜帶 用戶(hù)認(rèn)證憑據(jù)(也就是允許客戶(hù)端發(fā)送的請(qǐng)求攜帶Cookie)?

header("Access-Control-Allow-Credentials:true");

寫(xiě)到方法內(nèi)部

2018年5月20日 19:08
編輯回答
心上人

PHP 代碼:

header('Access-Control-Allow-Origin:' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Methods:GET,POST,OPTIONS,PUT');
header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Authorization');

JS請(qǐng)求代碼:

$.ajax({
   type: "POST",
   xhrFields:{
              withCredentials:true
            },
   success: function(){
     
   }
});
2017年12月28日 06:51
編輯回答
吃藕丑

還是跨域的問(wèn)題沒(méi)有解決。

前端需要允許跨域,加入xhrFields,jQuery的話,大概這樣:

$.ajax({
   type: "POST",
   xhrFields:{
              withCredentials:true
            },
   success: function(){
     
   }
});

后端加入header

header("Access-Control-Allow-Credentials", "true"); 
header("Access-Control-Allow-Origin", "允許跨域的地址,如果全部允許寫(xiě)*"); 

2018年1月19日 12:04