Skip to content

Instantly share code, notes, and snippets.

@Oranzh
Last active October 28, 2021 07:25
Show Gist options
  • Save Oranzh/b6954e687a97d62fc241443318c1e827 to your computer and use it in GitHub Desktop.
Save Oranzh/b6954e687a97d62fc241443318c1e827 to your computer and use it in GitHub Desktop.
CORS 本地环境跨域测试方法
1.为什么要进行跨域测试?
因为一些(跨域)的请求会被同源策略禁止,比如Ajax请求
跨域资源共享(英语:Cross-origin resource sharing,缩写:CORS),用于让网页的受限资源能够被其他域名的页面访问的一种机制。
主要原因是我想测试Lumen的Cors中间件(https://github.com/palanik/lumen-cors)。
2.如何在本地环境进行测试?
谷歌浏览器(为什么是谷歌,因为我只用google浏览器)的开发者工具,打开console,输入以下JS代码
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1:8080/posts/2');
xhr.send(null);
xhr.onload = function(e) {
var xhr = e.target;
console.log(xhr.responseText);
}
@Oranzh
Copy link
Author

Oranzh commented Nov 2, 2020

两次测试对比,第二次设置的允许跨域

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment