Created
October 10, 2010 18:25
-
-
Save think49/619437 to your computer and use it in GitHub Desktop.
http.js : XMLHttpRequest を利用してHTTPリクエストする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var XMLHttpRequest = (typeof XMLHttpRequest === 'function' || typeof XMLHttpRequest === 'object') ? XMLHttpRequest : (function () { | |
var i, l; | |
for (i = 0, l = arguments.length; i < l; i++) { | |
try { | |
arguments[i](); | |
return arguments[i]; | |
} | |
catch (error) {} | |
} | |
throw new Error('XMLHttpRequest is not defined'); | |
})(function () { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); }, function () { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); }); | |
// HTTP constructor | |
function HTTP () { | |
if (!(this instanceof HTTP)) { | |
throw new Error(this + ' is not a object created by constructor'); | |
} | |
return this; | |
} | |
// HTTP.prototype | |
(function () { | |
// createReadystatechangeHandler(callbackfn [, thisObject, xhr]) | |
function createReadystatechangeHandler (callbackfn, thisObject, xhr) { | |
var listener; | |
if (typeof callbackfn !== 'function') { | |
throw new TypeError(callbackfn + ' is not a function'); | |
} | |
listener = function (event) { | |
var responseText, responseXML; | |
if (event && event.target) { | |
xhr = event.target; | |
} | |
if (typeof xhr !== 'object') { | |
throw new TypeError(xhr + ' is not a object'); | |
} | |
if (xhr.readyState === 4) { | |
if (xhr.status === 200) { | |
responseText = xhr.responseText; | |
responseXML = xhr.responseXML; | |
if (responseText || responseXML) { | |
if (thisObject) { | |
callbackfn.call(thisObject, responseText, responseXML); | |
} else { | |
callbackfn(responseText, responseXML); | |
} | |
} | |
} | |
// xhr.abort(); // Google Chrome 6 returns an error : XMLHttpRequest Exception 101 | |
if (xhr.removeEventListener) { | |
xhr.removeEventListener('readystatechange', listener, false); | |
} else { | |
xhr.onreadystatechange = new Function; | |
} | |
callbackfn = thisObject = xhr = listener = null; | |
} | |
}; | |
return listener; | |
} | |
// HTTP.prototype.request(method, url [, callbackfn, options]) | |
this.request = function (method, url, callbackfn, options) { | |
var async, data, thisObject, xhr, isDocument; | |
// default value | |
async = false; | |
data = null; | |
if (options !== null && typeof options === 'object') { | |
if ('async' in options) { | |
async = options.async; | |
} | |
if ('data' in options) { | |
data = options.data; | |
} | |
thisObject = options.thisObject; | |
} | |
if (typeof url !== 'string') { | |
throw new TypeError(method + ' is not a string'); | |
} | |
// RFC 2616 (HTTP/1.1) | |
if (method !== 'GET' && method !== 'POST' && method !== 'HEAD' && method !== 'PUT' && method !== 'OPTIONS' && method !== 'DELTE' && method !== 'TRACE' && method !== 'CONNECT') { | |
throw new TypeError(method + ' is not a method (RFC 2616)'); | |
} | |
if (method === 'GET' || method === 'HEAD') { // XMLHttpRequest (3.6.3. The send() method) | |
data = null; | |
} | |
if (data !== null && typeof data === 'object' && data.nodeType === 9) { | |
isDocument = true; | |
} | |
if (data !== null && !isDocument && typeof data !== 'string') { // XMLHttpRequest (3.6.3. The send() method) | |
throw new TypeError(data + ' is not a null or document or string'); | |
} | |
async = Boolean(async); | |
xhr = new XMLHttpRequest; | |
xhr.open(method, url, async); | |
if (typeof callbackfn === 'function') { | |
if (xhr.addEventListener) { | |
xhr.addEventListener('readystatechange', createReadystatechangeHandler(callbackfn, thisObject), false); // XMLHttpRequest (3.4. Event Handler Attributes) | |
} else { | |
xhr.onreadystatechange = createReadystatechangeHandler(callbackfn, thisObject, xhr); // [IE8] Handler's thisObject is not [object XMLHttpRequest] | |
} | |
} | |
xhr.setRequestHeader('Pragma', 'no-cache'); | |
xhr.setRequestHeader('Cache-Control', 'no-cache'); | |
xhr.setRequestHeader('If-Modified-Since', 'Thu, 01 Jun 1970 00:00:00 GMT'); | |
// XMLHttpRequest (3.6.3. The send() method) | |
if (isDocument) { | |
xhr.setRequestHeader('Content-Type', 'application/xml'); | |
} else if (method === 'POST') { | |
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
} else if (typeof data === 'string') { | |
xhr.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8'); | |
} | |
xhr.send(data); | |
}; | |
// HTTP.prototype.get(url [, callbackfn, options]) | |
this.get = function (url, callbackfn, options) { | |
this.request('GET', url, callbackfn, options); | |
}; | |
}).call(HTTP.prototype); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment