cat 方式 ret turn 访问 || 指定 define
CORS(Cross-Origin Resource Sharing, 跨源资源共享) 是 W3C 出的一个标准,其思想是使用自定义的 HTTP 头部让浏览器与服务器进行沟通,从而决定请求或响应是应该成功,还是应该失败。
使用 CORS 时服务器需设置 Access-Control-Allow-Origin 头部为'*'或者某一个或几个域。
其中设为 * 的话表示所有的域都可以对此服务器进行访问,设置为某一个域的情况下便只有该域的请求可以访问。
带凭据的请求:
默认情况下,跨域请求不提供凭据(cookie、HTTP 认证及客户端 SSL 证明等)。
在发送请求时,通过将 withCredentials 属性设置为 true,可以指定某个请求可以发送凭据。
如果服务器的 Response Headers 中返回 Access-Control-Allow-Credentials:true,则表示服务端接受带凭据的请求
CORS 的跨浏览器实现方式:
- function createCORSRequest(method, url) {
- varxhr =new XMLHttpRequest();
- xhr.onload =function() {
- if(xhr.readyState == 4) {
- try {
- if((xhr.status >= 200 && xhr.status < 300) || xhr == 304) {
- console.log(xhr.response);
- } else {
- console.log('Request was unsuccessful: ' + xhr.status);
- }
- } catch(ex) {
- new Error(ex);
- }
- }
- };
- if('withCredentials'in xhr) {
- xhr.open(method,url, true);
- } else if(typeofXDomainRequest != 'undefined') {
- xhr =new XDomainRequest();
- xhr.open(method, url);
- } else {
- xhr =null;
- }
- return xhr;
- }
之后会补上服务端返回字段的意义和 CORS 简单请求和非简单请求的方式。
晚安
关于 CORS 的一点研究
来源: http://www.bubuko.com/infodetail-2015941.html