Ajax带登录凭据跨域请求
在远端设置header,此处使用Laravel的中间件中。
return $next($request) -> header('Access-Control-Allow-Origin', $domain) -> header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') -> header('Access-Control-Allow-Credentials','true');
ACAO、ACAM、ACAC这三个头比不可少,分别是允许跨的域,跨域请求的方式,是否允许携带请求凭据。
对于近端,你只需要withCredentials = true;即可
var x=new XMLHttpRequest(); x.open('GET','https://xcroot.com',1); x.withCredentials = true; x.onload=function(){ console.log(x.responseText); }; x.send();
参考http://www.w3.org/TR/XMLHttpRequest/#the-withcredentials-attribute
页面下部广告
this useful, thx.