此一生 一個純js的ajax

來源:互聯網
上載者:User

標籤:style   blog   class   code   c   java   

/** * 得到ajax對象 */function getajaxHttp() {    var xmlHttp;    try {        // Firefox, Opera 8.0+, Safari        xmlHttp = new XMLHttpRequest();        } catch (e) {            // Internet Explorer            try {                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");            } catch (e) {            try {                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");            } catch (e) {                alert("您的瀏覽器不支援AJAX!");                return false;            }        }    }    return xmlHttp;}/** * 發送ajax請求 * url--url * methodtype(post/get) * con (true(非同步)|false(同步)) * parameter(參數) * functionName(回調方法名,不需要引號,這裡只有成功的時候才調用) * (注意:這方法有二個參數,一個就是xmlhttp,一個就是要處理的對象) * obj需要到回調方法中處理的對象 */function ajaxrequest(url,methodtype,con,parameter,functionName,obj){    var xmlhttp=getajaxHttp();    xmlhttp.onreadystatechange=function(){        if(xmlhttp.readyState==4){            //HTTP響應已經完全接收才調用            functionName(xmlhttp,obj);        }    };    xmlhttp.open(methodtype,url,con);    xmlhttp.send(parameter);}//這就是參數function createxml(){    var xml="<user><userid>asdfasdfasdf<\/userid><\/user>";//"\/"這不是大寫V而是轉義是左斜杠和右斜杠    return xml;}//這就是參數function createjson(){    var json={id:0,username:"好人"};    return json;}function c(){    alert("");}//測試ajaxrequest("http://www.baidu.com","post",true,createxml(),c,document);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.