Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Created December 10, 2012 15:52
Show Gist options
  • Save MikeRogers0/4251395 to your computer and use it in GitHub Desktop.
Save MikeRogers0/4251395 to your computer and use it in GitHub Desktop.
Returns value from test.php file
function doSomething(xhr) {
var test = xhr.responseText + 'was found';
alert(test);
}
function ajaxReturn (file,func,data) {
var xhr = new XMLHttpRequest();
xhr.open("POST",file, false); // Adding in the false, means send will wait for a reply until continuing with the code.
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(data);
if (xhr.readyState === 4 && xhr.status === 200) {
func(xhr);
}
}
ajaxReturn('test.php',doSomething);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment