
自己用 XHR 对象向 google.com 发起 get 请求,浏览器会报
Access to XMLHttpRequest at 'https://www.google.com/' from origin 'http://localhost:52331' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 我用浏览器直接访问 google.com ,不也是在不同的域吗,为什么没有跨域问题。
1 lhx2008 2019 年 12 月 22 日 不要带自定义头 |
2 lhx2008 2019 年 12 月 22 日 说错了,这种 Google 回的时候要有一个额外的头 |
3 leosirius666a OP 我什么都没做,代码这样的 ```js let xhr = new XMLHttpRequest(); xhr.open("get", "https://www.google.com", true); xhr.send(null); xhr.Onreadystatechange= function () { if (xhr.readyState == 4) { if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { alert(xhr.responseText); } else { alert("Request was unsuccessful: " + xhr.status); } } } ``` |
4 1OF7G 2019 年 12 月 22 日 这是浏览器对网页施加的限制,而不是 系统或网络协议或某种魔法 对浏览器的限制。 |
5 leosirius666a OP @1OF7G 我知道是浏览器在 response 成功返回后浏览器自己把返回 block 了,但是为什么直接访问它就没有 block |
6 1OF7G 2019 年 12 月 22 日 @leosirius666a 浏览器只是阻止把 google.com 的内容给到其它第三方网站的 JS,如你的 localhost。如果你直接访问,根本没有第三者参与进来,还防范谁呢? |
7 beastk 2019 年 12 月 22 日 via iPhone js 限制 |
8 VDimos 2019 年 12 月 22 日 via Android 。。。跨域不是这个意思,跨域是指你的域名的脚本加载了不是你域名的内容。。。 |
9 leosirius666a OP @VDimos 原来是这样。。 |
10 zsdroid 2019 年 12 月 22 日 |
11 cst4you 2019 年 12 月 22 日 |
12 leosirius666a OP |
13 ssshooter 2019 年 12 月 22 日 |
14 x66 2019 年 12 月 22 日 “chrome.exe” disable-web-security user-data-dir=xxx |
15 beastk 2019 年 12 月 22 日 via iPhone 其实你还没搞懂。localhost 是本地域,有特权,不信你用 ie 使用 ajax,http https 是网络域,脚本资源遵循同源策略。 |