为何浏览器要阻止不带 cookie 的跨域请求的响应?
在 Google 页面执行:
xhr=new XMLHttpRequest
xhr.addEventListener('load',()=>console.log('xhr load'))
xhr.open("GET","https://www.baidu.com")
xhr.withCredentials = false; // 不发送 cookie
xhr.send(null)
响应依然被浏览器阻止:
XMLHttpRequest cannot load https://www.baidu.com/.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'https://www.google.com' is therefore not allowed access.
但是 IE8 里的 XDomainRequest 不发送 cookie,也不会阻止。
想访问一些网站的公开 API,可是这些网站的响应又不带 Access-Control-Allow-Origin,只能用 JSONP,有些不爽。
所以我想问为何浏览器要阻止不带 cookie 的跨域 XHR 请求的响应?有什么安全方面的风险?