CORS

CORS

IE8中

在IE8中用XDR来实现安全可靠的跨域通信

1.	cookies不会随请求发送,也不随响应返回

2.	只能设置请求头的content-type字段

3.	不能访问响应头信息

4.	只支持get,post

这些变化可以使CSRF和XSS的问题得到缓解

它的open只有两个参数

xdr.open("get","http://....")

如果缺少Access-Control-Allow-Origin头部,就会触发error事件

xdr.onerror = function(){}

xdr跟xhr一样有timeout,abort(终止请求)

其他浏览器的CORS

与IE的XDR不同,跨域XHR可以访问status和statusText属性,还支持同步

1.	不能使用setRequestHeader()设置自定义头部
2.	不能发送和接收cookies
3.	调用getAllResponseHeaders()方法会返回空字符串

检查xhr支不支持CORS

通过检查 withCredentials属性

	function createCORSRequest(method,url){
		var xhr = new XMLHttpRequest();
		if("withCredentials" in xhr){
			xhr.open(method,url,true)
		}else if(typeof XDomainRequest != "undefined"){
			xhr = new XDomainRequest();
			xhr.open(method,url);
		}else{
			xhr = null;
		}
		return xhr;
	}

posted on 2017-12-21 11:06  ouruixi  阅读(151)  评论(0)    收藏  举报

导航