Last active
February 27, 2016 03:12
-
-
Save JosePedroDias/2d4bf89ccb4e5aaee9a4 to your computer and use it in GitHub Desktop.
AJAX advanced
This file contains 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
function ajax(o) { | |
'use strict'; | |
var xhr = new XMLHttpRequest(); | |
if (o.withCredentials) { xhr.withCredentials = true; } | |
xhr.open(o.method || 'GET', o.url, true); | |
var cbInner = function() { | |
if (xhr.readyState === 4 && xhr.status > 199 && xhr.status < 300) { | |
return o.cb(null, JSON.parse(xhr.response)); | |
} | |
o.cb('error requesting ' + o.url); | |
}; | |
xhr.onload = cbInner; | |
xhr.onerror = cbInner; | |
xhr.send(o.body || null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment