Created
August 25, 2011 17:06
-
-
Save brianleroux/1171168 to your computer and use it in GitHub Desktop.
quick xhr post example
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
// USAGE | |
// post('http://example.com', {name:'brian'}, console.log) | |
var post = function(url, params, cb) { | |
var x = new XMLHttpRequest() | |
, p = '' | |
x.onreadystatechange = function() { | |
if (x.readyState === 4) | |
cb.call(this, x.status) | |
} | |
for (var i in params) | |
p += i +'='+ params[i] +'&' | |
x.open("POST", url, true) | |
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded") | |
x.send(p.substring(0, params.length-1)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this is an OLD gist, but there is a typo. The line:
x.send(p.substring(0, params.length-1))
should be:
x.send(p.substring(0, p.length-1))
Hope that helps anyone who runs across this!