不知道你是否跟錯新技術頻道的小編一樣,給自己定個小目標,在年前寫個一個封裝的AJAX類,AJAX已然成為了前端開發最重要的“工具”之一,快跟隨錯新小編一起往下了解吧。
用法:?
new?Ajax().Request(url,cmd,async,method,postString,title)?
參數:?
url:?請求頁面URL(必填)?
cmd:?返回值處理函數(必填)?
async:?是否異步?,(ture|false),?默認true?
method:?請求方式,(post|get),?默認get?
postString:?請求方式為post時,請求內容?
title:?請求內容標題?
"?+ ??????????????????" ![]() ????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 ????????} ????} ????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="正在獲取數據";?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("正在發送數據..."); ????????????????return; ????????????case?3: ????????????????msgbox.showmsg("正在接收數據..."); ????????????????return; ????????????case?4: ????????????????msgbox.showmsg("數據接收完成..."); ????????????????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("正在接收數據...");x.send(postString);}?else?x.send(); ????????if?(!async)?{ ????????????msgbox.showmsg("數據接收完成..."); ????????????cmd(x.responseText); ????????????msgbox.clear(); ????????} ????}???? } | ?style=/"padding:2px;border-top:1px?solid?#e1e1e1;border-left:1px?solid?#e1e1e1;border-bottom:1px?solid?#818181;border-right:1px?solid?#a1a1a1/">
錯新技術小編已經成功完成了年前定下的目標,寫下這篇一個封裝的AJAX類,你也要努力了喔,更多關于AJAX的內容,可以多多關注下錯新技術頻道,我們會不斷的更新。
新聞熱點
疑難解答
圖片精選