iT邦幫忙

0

請問這兩種Ajax的寫法有什麼差別呢?

小弟目前比較常用的寫法,也是在書上學到的寫法是這樣的

window.XMLHttpRequest ? xmlhttp=new XMLHttpRequest() : xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","forum_ajax.php",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
	if(xmlhttp.readyS	tate==4 && xmlhttp.status==200){
		$("#result").html(xmlhttp.responseText);
	}
}		

但網路上常常看到有人用這種寫法

$.ajax({
    url:"forum_ajax.php",
type:'GET',
success: function(data) {
	$("#result").html(data);
},
error: function() {
    alert('error');
    }
});	

請問這兩種寫法有什麼差異呢?
若使用第二種寫法,需要宣告這個物件嗎?

window.XMLHttpRequest ? xmlhttp=new XMLHttpRequest() : xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
fillano
iT邦超人 1 級 ‧ 2015-03-27 20:23:33
最佳解答

第二個方式是呼叫jQuery的ajax方法來做的。

我要發表回答

立即登入回答