好用的AJAX類代碼分享
更新時間:2011年11月29日 22:36:06 作者:
好用的AJAX類代碼分享,需要的朋友可以參考下。
ajax.js
-------------------------[ajax類]--------------------------
function Ajax(recvType){
var aj=new Object();
aj.recvType=recvType ? recvType.toUpperCase() : 'HTML'; //向形參中傳遞的文件類型
aj.targetUrl='';
aj.sendString='';
aj.resultHandle=null;
/*創(chuàng)建XMLHttpRequest對象*/
aj.createXMLHttpRequest=function(){
var xmlHttp = false;
if(window.XMLHttpRequest){ //在非IE中創(chuàng)建XMLHttpRequest對象
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //按新版IE創(chuàng)建
}catch(error1){ //創(chuàng)建失敗
try{
xmlHttp = new ActiveXobject("Microsoft.XMLHttp"); //按老版IE創(chuàng)建
}catch(error2){ //創(chuàng)建失敗
xmlHttp = false;
}
}
}
return xmlHttp;
}
aj.XMLHttpRequest=aj.createXMLHttpRequest();
/*處理服務器的響應*/
aj.processHandle=function(){
if(aj.XMLHttpRequest.readyState == 4){
if(aj.XMLHttpRequest.status == 200){
if(aj.recvType=="HTML")
aj.resultHandle(aj.XMLHttpRequest.responseText);
else if(aj.recvType=="XML")
aj.resultHandle(aj.XMLHttpRequest.responseXML);
}
}
}
/*定義使用get方法傳遞的方法*/
aj.get=function(targetUrl, resultHandle){
aj.targetUrl=targetUrl;
if(resultHandle!=null){
aj.XMLHttpRequest.onreadystatechange=aj.processHandle;
aj.resultHandle=resultHandle;
}
if(window.XMLHttpRequest){
aj.XMLHttpRequest.open("get", aj.targetUrl);
aj.XMLHttpRequest.send(null);
}else{
aj.XMLHttpRequest.open("get", aj.targetUrl, true);
aj.XMLHttpRequest.send();
}
}
/*定義使用post方法傳遞的方法*/
aj.post=function(targetUrl, sendString, resultHandle){
aj.targetUrl=targetUrl;
if(typeof(sendString)=="object"){
var str="";
for(var pro in sendString){
str+=pro+"="+sendString[pro]+"&";
}
aj.sendString=str.substr(0, str.length-1);
}else{
aj.sendString=sendString;
}
if(resultHandle!=null){
aj.XMLHttpRequest.onreadystatechange=aj.processHandle;
aj.resultHandle=resultHandle;
}
aj.XMLHttpRequest.open("post", targetUrl);
aj.XMLHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
aj.XMLHttpRequest.send(aj.sendString);
}
return aj;
}
-------------------------[使用方法]--------------------------
useAjax.html
<script src="ajax.js"></script>
<script language="javascript" type="text/javascript">
var ajax=Ajax();
/*get使用方式*/
ajax.get("server.php?name=zhangsan&phone=778", function(data){
alert(data); //data為從服務器端讀取的數(shù)據(jù)
});
/*第一種post使用方式*/
/* ajax.post("server.php", "name=ligang&phone=222", function(data){
alert(data);
});
*/
/*第二種post使用方式*/
/* ajax.post("server.php", {name:"tom",phone:"456"},function(data){
alert(data);
});
*/
</script>
server.php
<?php
header("Content-type:text/html;charset=gb2312");
$str = "姓名:{$_GET["name"]}\n電話:{$_GET["phone"]}";
echo $str;
?>
在瀏覽器中輸入useAjax.html的地址,若出現(xiàn)
-------------------------[ajax類]--------------------------
復制代碼 代碼如下:
function Ajax(recvType){
var aj=new Object();
aj.recvType=recvType ? recvType.toUpperCase() : 'HTML'; //向形參中傳遞的文件類型
aj.targetUrl='';
aj.sendString='';
aj.resultHandle=null;
/*創(chuàng)建XMLHttpRequest對象*/
aj.createXMLHttpRequest=function(){
var xmlHttp = false;
if(window.XMLHttpRequest){ //在非IE中創(chuàng)建XMLHttpRequest對象
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //按新版IE創(chuàng)建
}catch(error1){ //創(chuàng)建失敗
try{
xmlHttp = new ActiveXobject("Microsoft.XMLHttp"); //按老版IE創(chuàng)建
}catch(error2){ //創(chuàng)建失敗
xmlHttp = false;
}
}
}
return xmlHttp;
}
aj.XMLHttpRequest=aj.createXMLHttpRequest();
/*處理服務器的響應*/
aj.processHandle=function(){
if(aj.XMLHttpRequest.readyState == 4){
if(aj.XMLHttpRequest.status == 200){
if(aj.recvType=="HTML")
aj.resultHandle(aj.XMLHttpRequest.responseText);
else if(aj.recvType=="XML")
aj.resultHandle(aj.XMLHttpRequest.responseXML);
}
}
}
/*定義使用get方法傳遞的方法*/
aj.get=function(targetUrl, resultHandle){
aj.targetUrl=targetUrl;
if(resultHandle!=null){
aj.XMLHttpRequest.onreadystatechange=aj.processHandle;
aj.resultHandle=resultHandle;
}
if(window.XMLHttpRequest){
aj.XMLHttpRequest.open("get", aj.targetUrl);
aj.XMLHttpRequest.send(null);
}else{
aj.XMLHttpRequest.open("get", aj.targetUrl, true);
aj.XMLHttpRequest.send();
}
}
/*定義使用post方法傳遞的方法*/
aj.post=function(targetUrl, sendString, resultHandle){
aj.targetUrl=targetUrl;
if(typeof(sendString)=="object"){
var str="";
for(var pro in sendString){
str+=pro+"="+sendString[pro]+"&";
}
aj.sendString=str.substr(0, str.length-1);
}else{
aj.sendString=sendString;
}
if(resultHandle!=null){
aj.XMLHttpRequest.onreadystatechange=aj.processHandle;
aj.resultHandle=resultHandle;
}
aj.XMLHttpRequest.open("post", targetUrl);
aj.XMLHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
aj.XMLHttpRequest.send(aj.sendString);
}
return aj;
}
-------------------------[使用方法]--------------------------
useAjax.html
復制代碼 代碼如下:
<script src="ajax.js"></script>
<script language="javascript" type="text/javascript">
var ajax=Ajax();
/*get使用方式*/
ajax.get("server.php?name=zhangsan&phone=778", function(data){
alert(data); //data為從服務器端讀取的數(shù)據(jù)
});
/*第一種post使用方式*/
/* ajax.post("server.php", "name=ligang&phone=222", function(data){
alert(data);
});
*/
/*第二種post使用方式*/
/* ajax.post("server.php", {name:"tom",phone:"456"},function(data){
alert(data);
});
*/
</script>
server.php
<?php
header("Content-type:text/html;charset=gb2312");
$str = "姓名:{$_GET["name"]}\n電話:{$_GET["phone"]}";
echo $str;
?>
在瀏覽器中輸入useAjax.html的地址,若出現(xiàn)
則Ajax方法使用正確
相關文章
通過抓取淘寶評論為例講解Python爬取ajax動態(tài)生成的數(shù)據(jù)(經典)
在學習python的時候,一定會遇到網站內容是通過 ajax動態(tài)請求、異步刷新生成的json數(shù)據(jù) 的情況,并且通過python使用之前爬取靜態(tài)網頁內容的方式是不可以實現(xiàn)的,所以這篇文章將要講述如果在python中爬取ajax動態(tài)生成的數(shù)據(jù)。2015-10-10詳解ajax +jtemplate實現(xiàn)動態(tài)分頁
jtemplate是一個基于JQuery的模板引擎插件,功能非常強大,有了她你就再不用為使用JS綁定數(shù)據(jù)集而發(fā)愁了。本文給大家分享ajax +jtemplate實現(xiàn)動態(tài)分頁,需要的朋友可以參考下本文2015-09-09如何利用jQuery post傳遞含特殊字符的數(shù)據(jù)
在jquery中,解決數(shù)據(jù)傳遞處理的方法我們通常利用$.ajax或$.post,但是這里這里通常不能傳遞特殊字符,比如說:“<”,本文就幫大家解決如何傳遞這種含特殊字符的數(shù)據(jù),感興趣的朋友一起看下吧2015-10-10