$.ajax({ url : 'http://remote.domain.com/corsrequest', data : data, dataType: 'json', type : 'POST', xhrFields: { withCredentials: true }, crossDomain: true, contentType: "application/json", ...
經過設置 withCredentials: true
,發送Ajax時,Request header中便會帶上 Cookie 信息。javascript
對應客戶端的 xhrFields.withCredentials: true
參數,服務器端經過在響應 header 中設置 Access-Control-Allow-Credentials = true
來運行客戶端攜帶證書式訪問。經過對 Credentials 參數的設置,就能夠保持跨域 Ajax 時的 Cookie。這裏須要注意的是:html
服務器端 Access-Control-Allow-Credentials = true
時,參數Access-Control-Allow-Origin
的值不能爲 '*'
。java
關於axiso的跨域攜帶cookie見https://www.cnblogs.com/yihuite-zch/p/10670364.htmlajax