发表于: 2015-08-20分类于:JavaScript


function createXHR(){
	if(typeof XMLHttpRequest != 'undefined'){
		return new XMLHttpRequest();
		if(typeof ActiveXObject != 'undefined'){   // IE < 7
			var versions = ['MSXML2.XMLHttp.6.0','MSXML2.XMLHttp.3.0','MSXML2.XMLHttp'];
			var i,len;
			for(i=0,len = versions.length;i<len;i++){
					new ActiveXObject(versions[i]);
					arguments.callee.activeXString = versions[i];
		return new ActiveXObject(arguments.callee.activeXString);
		throw new Error('No XHR object available.');


  • open(method,URL,asyncFlag,username,password);if set asyncFlag to true,means that request will be send for execution without waiting for a response.
  • send(content)
  • overrideMineType(MIME); // invoke before send
  • setRequestHeader


  • responseText
  • responseXML: if the mime type of request object is text/xml or application/xml , this value will save XML DOM

  • readyState
    • 0:uninitialized
    • 1:loading
    • 2:loaded
    • 3:interactive
    • 4:complete

if readyState change there will be a readystatechange event happen

  • abort: can use this method cancel request before receive response

HTTP hander

  • Accept: the type which broswer can process
  • Accept-Charset
  • Accept-Encoding
  • Accept-Language
  • Connection: the connection type between client and server
  • Cookie
  • Host : domain of current page
  • Referer:url of current page (right spell is referrer)
  • User-Agent

we can call setRequestHeader method set request header after open but before send

we can get header information thought getResponseHeader,we can get whole header item by getAllResponseHeader

cross domain

XHR set a Origin item,if server response a Access-Control-Allow-Origin ,can contain same value which the Origin of XHR seted of contain a character *. broswer allow this cross domain request.


  • load
  • abort
  • timeout
  • error

only one event occur ,after it a loadend evnet will happen.