function myCallBack(xmlhttp) {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
console.log(xmlhttp.responseText);
}
}
function start() {
var xmlhttp = new XMLHttpRequest();
var contentDiv = document.getElementById("Content");
xmlhttp.open("POST", "Demo", true);
xmlhttp.onreadystatechange=function() {
myCallBack(xmlhttp);
};
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
xmlhttp.send("FirstName=Nat&LastName=Dunn");
}
Why can"t the above code be written as
function myCallBack(xmlhttp) {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
console.log(xmlhttp.responseText);
}
}
function start() {
var xmlhttp = new XMLHttpRequest();
var contentDiv = document.getElementById("Content");
xmlhttp.open("POST", "Demo", true);
xmlhttp.onreadystatechange=myCallBack(xmlhttp);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
xmlhttp.send("FirstName=Nat&LastName=Dunn");
}
the difference between the two pieces of code
xmlhttp.onreadystatechange=myCallBack(xmlhttp);
xmlhttp.onreadystatechange=function() {
myCallBack(xmlhttp);
};