samhuri.net/assets/request.js
Sami Samhuri 94bf683fb1 commenting system is almost ready for primetime
* switched to CORS from JSONP
* improved style
* separated almost all JavaScript from the HTML
* minify & combine JavaScript using closure & cat
* fleshed out Makefile
2010-12-18 02:25:54 -08:00

31 lines
1 KiB
JavaScript

;(function() {
if (typeof SJS === 'undefined') SJS = {}
// cors xhr request, quacks like mikeal's request module
SJS.request = function(options, cb) {
var url = options.uri
, method = options.method || 'GET'
, headers = options.headers || {}
, body = typeof options.body === 'undefined' ? null : String(options.body)
, xhr = new XMLHttpRequest()
// withCredentials => cors
if ('withCredentials' in xhr) {
xhr.open(method, url, true)
} else if (typeof XDomainRequest === 'functon') {
xhr = new XDomainRequest()
xhr.open(method, url)
} else {
cb(new Error('cross domain requests not supported'))
return
}
for (var k in headers) if (headers.hasOwnProperty(k)) {
xhr.setRequestHeader(k, headers[k])
}
xhr.onload = function() {
var response = xhr.responseText
cb(null, xhr, response)
}
xhr.send(body)
}
}());