Created
June 16, 2013 08:19
-
-
Save felixzapata/5791368 to your computer and use it in GitHub Desktop.
JavaScript AJAX request
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
function ajax(url, opts){ | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function(){ | |
var completed = 4; | |
if(xhr.readyState === completed){ | |
if(xhr.status === 200){ | |
opts.success(xhr.responseText, xhr); | |
}else{ | |
opts.error(xhr.responseText, xhr); | |
} | |
} | |
} | |
xhr.open(opts.method, url, true); | |
xhr.send(opts.data); | |
} | |
ajax('/foo', { | |
method: 'GET', | |
success: function(response){ | |
console.log(response); | |
}, | |
error: function(response){ | |
console.log(response); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment