Skip to content

Instantly share code, notes, and snippets.

@flymrc
Forked from Oranzh/CORS TEST
Created October 28, 2021 07:25
Show Gist options
  • Save flymrc/e9b531d08e44cba58f3011ad603a0c2a to your computer and use it in GitHub Desktop.
Save flymrc/e9b531d08e44cba58f3011ad603a0c2a 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment