日期:2014-05-16  浏览次数:20449 次

AJAX各种js实现方式

?

1.传统的js实现方式:

?

?写道
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp = new XMLHttpRequest();
}
function doRequestUsingGET(){
createXMLHttpRequest();
var queryString = "9-3.aspx?";
queryString += createQueryString() + "&timestamp=" + new Date().getTime();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET",queryString);
xmlHttp.send(null);
}
function doRequestUsingPOST(){
createXMLHttpRequest();
var url = "9-3.aspx?timestamp=" + new Date().getTime();
var queryString = createQueryString();
xmlHttp.open("POST",url);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(queryString);
}
</script>
?

2.JQuery的load方法

?

$(selector).load(url,data,function(response,status,xhr))

详情见:http://www.w3schools.com/jquery/ajax_load.asp

?

3.JQuery的get与post方法

?

$(selector).get(url,data,success(response,status,xhr),dataType)

详情:http://www.w3schools.com/jquery/ajax_get.asp

?

$(selector).post(url,data,success(response,status,xhr),dataType)

?

4.Jquery的ajax方法

这种方法更灵活。可以设置各种细节

$.ajax({name:value, name:value, ... })

http://www.w3schools.com/jquery/ajax_ajax.asp

?