Comparing version 2.0.3 to 3.0.0
var XMLHttpRequest = require('xhrpolyfill') | ||
module.exports = function xhr2 (params, callback) { | ||
// returns/calls: callback(error, data, response, xhr) | ||
var args = { // e.g. url = "http://jsonplaceholder.typicode.com/posts/1" | ||
url : typeof params === 'string' ? params : params.url, | ||
method : params.method || (params.data ? 'POST': 'GET'), | ||
body : params.data, // data: formdata or {key:val} | ||
timeout : params.timeout || 0, | ||
headers : (function(){ | ||
var header = { | ||
'X-Requested-With' :'XMLHttpRequest', | ||
'Content-Type' :'application/x-www-form-urlencoded' | ||
} | ||
return params.headers ? params.headers : params.body ? header : {} | ||
})() | ||
} | ||
// returns/calls: callback(data, response, xhr) | ||
var url = typeof params === 'string' ? params : params.url | ||
// e.g. url = "http://jsonplaceholder.typicode.com/posts/1" | ||
var method = params.method || (params.data ? 'POST': 'GET') | ||
var body = params.data // data: formdata or {key:val} | ||
var H = params.headers ? params.headers : params.body ? { | ||
'X-Requested-With' :'XMLHttpRequest', | ||
'Content-Type' :'application/x-www-form-urlencoded' | ||
} : {} | ||
var xhr = XMLHttpRequest() | ||
if (!xhr) throw new Error('No AJAX support') | ||
xhr.open(args.method, args.url) | ||
for (var field in args.headers) { | ||
xhr.setRequestHeader(field, args.headers[field]) | ||
} | ||
xhr.onload = xhr.onerror = function responseHandler (response){ | ||
clearTimeout(tt) | ||
var headerJSON = {}, h = xhr.getAllResponseHeaders() | ||
xhr.open(method, url) | ||
for (var field in H) xhr.setRequestHeader(field, H[field]) | ||
xhr.onload = xhr.onerror = function (response) { | ||
var Hjson = {}, h = xhr.getAllResponseHeaders() | ||
;(h.match(/([^\n\r:]+):([^\n\r]+)/g)||[]).forEach(function(item){ | ||
var tmp = item.split(': ') | ||
headerJSON[tmp[0]] = tmp[1] | ||
Hjson[tmp[0]] = tmp[1] | ||
}) | ||
if (callback) callback(null, this.response, response, xhr, headerJSON) | ||
if (callback) callback(this.response, response, xhr, Hjson) | ||
} | ||
var tt | ||
xhr.ontimeout = function (error) { clearTimeout(tt); cancel(error) } | ||
if (args.timeout > 0 ) xhr.timeout = tt = setTimeout(cancel, args.timeout) | ||
function cancel (e) { | ||
xhr.abort("timeout") | ||
e = e ? e : new Error("XMLHttpRequest timeout") | ||
e.code = "ETIMEDOUT" | ||
if (callback) { callback(e); callback = function noop () {} } | ||
} | ||
xhr.send(args.body||null) | ||
} |
{ | ||
"name": "minixhr", | ||
"version": "2.0.3", | ||
"version": "3.0.0", | ||
"description": "super simpel and small cross-browser xhr", | ||
@@ -5,0 +5,0 @@ "main": "minixhr.js", |
@@ -17,3 +17,2 @@ # minixhr | ||
body : 'payload', // [optional] payload data could be <formdata> or {key:val}'s or anything | ||
timeout : 2000 // [optional] (to set a timeout) | ||
headers : {} // [optional] (defaults to '{}' or in case of 'POST': | ||
@@ -20,0 +19,0 @@ // {'X-Requested-With':'XMLHttpRequest','Content-Type':'application/x-www-form-urlencoded' } ) |
3563
25
32