Skip to content

Instantly share code, notes, and snippets.

@brianleroux
Created August 25, 2011 17:06
Show Gist options
  • Save brianleroux/1171168 to your computer and use it in GitHub Desktop.
Save brianleroux/1171168 to your computer and use it in GitHub Desktop.
quick xhr post example
// 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))
}
@rustyvz
Copy link

rustyvz commented Mar 22, 2017

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment