samhuri.net/assets/js/request.js
2011-12-03 14:25:31 -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)
}
}());