国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > AJAX > 正文

一個封裝的AJAX類

2024-09-01 08:30:17
字體:
來源:轉載
供稿:網(wǎng)友

不知道你是否跟錯新技術頻道的小編一樣,給自己定個小目標,在年前寫個一個封裝的AJAX類,AJAX已然成為了前端開發(fā)最重要的“工具”之一,快跟隨錯新小編一起往下了解吧。

用法:?
new?Ajax().Request(url,cmd,async,method,postString,title)?
參數(shù):?
url:?請求頁面URL(必填)?
cmd:?返回值處理函數(shù)(必填)?
async:?是否異步?,(ture|false),?默認true?
method:?請求方式,(post|get),?默認get?
postString:?請求方式為post時,請求內(nèi)容?
title:?請求內(nèi)容標題?

復制代碼 代碼如下:

//?Ajax?封裝?2007-3-13
function?createXMLHttpRequest()?{
????try?{????????
????????????if?(window.XMLHTTPRequest)?{
????????????????????return?new?XMLHttpRequest();
????????????}
????????????else?if?(window.ActiveXObject)?{
????????????????return?new?ActiveXObject("Microsoft.XMLHTTP");
????????????}
????????}
????????catch?(e)?{alert("XMLHttpRequest對象無法創(chuàng)建!請檢查IE安全設置!");}
}
function?messageDiv(t)
{
????var?v?=?document.createElement("
");
????v.innerHTML?=?""?+
??????????????????""?+?
??????????????????"?style=/"padding:2px;border-top:1px?solid?#e1e1e1;border-left:1px?solid?#e1e1e1;border-bottom:1px?solid?#818181;border-right:1px?solid?#a1a1a1/">?style=/"font-size:12px;background-color:#eeeeff;color:#227933;height:20px/">
"?+
??????????????????"?"?+?t?+?",連接未初始化...?id=span1>?>";
????var?l?=?document.getElementsByName("message").length;
????v.style.cssText?=?"position:absolute;bottom:"?+?(l*24)?+?"px;left:0px;display:none";
????document.body.appendChild(v);
????this.clear?=?function?()?{
????????document.body.removeChild(v);
????????var?msg?=?document.getElementsByName("message");
????????for?(var?i=0;i????????????msg[i].parentNode.style.cssText?=?"position:absolute;bottom:"?+?(i*24)?+?"px;left:0px";
????????}
????}
????this.showmsg?=?function?(s)?{
????????v.style.display?=?"";
????????v.all.Span1.innerHTML?=?s;
????}
}
function?Ajax()?{
????var?x?=?new?createXMLHttpRequest();
????this.Request?=?function?(url,cmd,async,method,postString,title)?{
????????if?(method!="post")?method?=?"post";?else?method?=?"get";
????????if?(async!=true)?async?=?true;?else?async?=?false;
????????if?(typeof(postString)!="string")?postString="";
????????if?(typeof(title)!="string")?title="正在獲取數(shù)據(jù)";?else?title="正在獲取"?+?title;
????????var?msgbox?=?new?messageDiv(title);
????????x.onreadystatechange?=?function?()
????????{
????????????if?(async)?switch?(x.readystate)?{
????????????case?1:
????????????????msgbox.showmsg("正在初始化連接...");
????????????????return;
????????????case?2:
????????????????msgbox.showmsg("正在發(fā)送數(shù)據(jù)...");
????????????????return;
????????????case?3:
????????????????msgbox.showmsg("正在接收數(shù)據(jù)...");
????????????????return;
????????????case?4:
????????????????msgbox.showmsg("數(shù)據(jù)接收完成...");
????????????????if?(x.status?==?200)?{
????????????????????cmd(x.responseText);
????????????????????msgbox.clear();
????????????????}
????????????????else?{
????????????????????msgbox.showmsg("請求失敗,"?+?x.statustext?+?"("?+?x.status?+?")");
????????????????????setTimeout(msgbox.clear,3000);
????????????????}
????????????????return;
????????????}
????????}
????????x.open?(method,url,async);
????????if?(method=="post")?{msgbox.showmsg("正在接收數(shù)據(jù)...");x.send(postString);}?else?x.send();
????????if?(!async)?{
????????????msgbox.showmsg("數(shù)據(jù)接收完成...");
????????????cmd(x.responseText);
????????????msgbox.clear();
????????}
????}????
}
;i++){>
?style=/"width:300px;/"?id=message>

錯新技術小編已經(jīng)成功完成了年前定下的目標,寫下這篇一個封裝的AJAX類,你也要努力了喔,更多關于AJAX的內(nèi)容,可以多多關注下錯新技術頻道,我們會不斷的更新。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 平顺县| 敦化市| 曲靖市| 峨山| 浦城县| 兴国县| 盈江县| 石城县| 荆州市| 玛纳斯县| 云浮市| 黔江区| 噶尔县| 沙坪坝区| 汤阴县| 安乡县| 临安市| 额尔古纳市| 佛学| 丹棱县| 方城县| 勃利县| 裕民县| 耒阳市| 鹿邑县| 攀枝花市| 丹巴县| 普洱| 元朗区| 济源市| 甘肃省| 政和县| 元氏县| 新化县| 东明县| 灯塔市| 淳化县| 桦甸市| 兴义市| 额敏县| 讷河市|