Skip to content

Instantly share code, notes, and snippets.

@Cathon
Created March 9, 2016 07:53
Show Gist options
  • Save Cathon/f64044a0d4d9b0305ae5 to your computer and use it in GitHub Desktop.
Save Cathon/f64044a0d4d9b0305ae5 to your computer and use it in GitHub Desktop.
function load() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState !== 4) return;
if (xhr.status !== 200) {
alert("Handle request failure here...");
return;
}
var res = JSON.parse(xhr.responseText);
console.log(res);
document.getElementById("name").innerHTML = res.name;
document.getElementById("sex").innerHTML = res.sex;
document.getElementById("age").innerHTML = res.age;
};
xhr.open("GET", "ajax.php", true);
// xhr.open("GET", "ajax.php?t=" + Math.random(), true);
// xhr.open("GET", "ajax.php?name=bill&sex=male", true);
xhr.send();
// xhr.open("POST","ajax.php",true);
// xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
// xhr.send("name=bill&sex=male");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment