呼叫AJAX的兩種寫法
寫法1:
var myObj;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", "json_demo.txt");
xmlhttp.send();
xmlhttp.onload = function() {
myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
};
寫法2:
var myObj;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
};
xmlhttp.open("GET", "json_demo.txt", true);
xmlhttp.send();
var myObj;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", "json_demo.txt");
xmlhttp.send();
xmlhttp.onload = function() {
myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
};
寫法2:
var myObj;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
};
xmlhttp.open("GET", "json_demo.txt", true);
xmlhttp.send();
留言
張貼留言