ajax XMLHttpRequest post get

來源:互聯網
上載者:User

/*在有中文參數時,接收方需要使用UTF-8方式對資料進行解碼
*不支援post附件
*/

function getXmlHttpRequest() {
    var xmlHttpRequest = null;
    try {
        xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e1) {
        try {
            xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            xmlHttpRequest = null;
        }
    }
    if (xmlHttpRequest == null && typeof(XMLHttpRequest) != 'undefined') {
        xmlHttpRequest = new XMLHttpRequest();
    }
    return xmlHttpRequest;
}

/*使用post方式發送資料
*url:submit路徑
*arguments:參數,如name=jese&sex=womanz,中文資料時使用'name='+getEncodeURIComponent('李四')+'&sex='+getEncodeURIComponent('男')進行編碼
*synchFlag:同步標記,false為同步方式,true為非同步方式
*returnType:返回內容類型0=responseBody;1=responseStream;2=responseText;3=responseXML
*execute:為非同步方式提交後的回呼函數,此函數用於處理返回資料,格式為function (obj){},obj為返回內容
*/

function postData(url, arguments, synchFlag, returnType, execute) {
    var xmlHttpRequest = getXmlHttpRequest();
    if (xmlHttpRequest == null) return false;
    xmlHttpRequest.open("POST", url, synchFlag);
    xmlHttpRequest.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
    xmlHttpRequest.setRequestHeader("Content-Length", arguments.length);
    if (synchFlag) {
        xmlHttpRequest.onreadystatechange = function() {
            if (xmlHttpRequest.readyState == 4) {
                switch (returnType) {
                    case 0: execute(xmlHttpRequest.responseBody); break;
                    case 1: execute(xmlHttpRequest.responseStream); break;
                    case 2: execute(xmlHttpRequest.responseText); break;
                    case 3: execute(xmlHttpRequest.responseXML); break;
                    default: alert("error");
                }
            }
        }
    }
    xmlHttpRequest.send(arguments);
    if (!synchFlag) {
        switch (returnType) {
            case 0: return xmlHttpRequest.responseBody; break;
            case 1: return xmlHttpRequest.responseStream; break;
            case 2: return xmlHttpRequest.responseText; break;
            case 3: return xmlHttpRequest.responseXML; break;
            default: return false;
        }
    }
}

/*
*使用get方式發送資料
*url:submit路徑
*arguments:參數,如name=jese&sex=男,中文資料部需要進行編碼
*synchFlag:同步標記,false為同步方式,true為非同步方式
*returnType:返回內容類型0=responseBody;1=responseStream;2=responseText;3=responseXML
*execute:為非同步方式提交後的回呼函數,此函數用於處理返回資料,格式為function (obj){},obj為返回內容
*/

function getData(url, arguments, synchFlag, returnType, execute) {
    var xmlHttpRequest = getXmlHttpRequest();
    if (xmlHttpRequest == null) return false;
    xmlHttpRequest.open("GET", url + "?" + arguments, synchFlag);
    if (synchFlag) {
        xmlHttpRequest.onreadystatechange = function() {
            if (xmlHttpRequest.readyState == 4) {
                switch (returnType) {
                    case 0: execute(xmlHttpRequest.responseBody); break;
                    case 1: execute(xmlHttpRequest.responseStream); break;
                    case 2: execute(xmlHttpRequest.responseText); break;
                    case 3: execute(xmlHttpRequest.responseXML); break;
                    default: alert("error");
                }
            }
        }
    }
    xmlHttpRequest.send();
    if (!synchFlag) {
        switch (returnType) {
            case 0: return xmlHttpRequest.responseBody; break;
            case 1: return xmlHttpRequest.responseStream; break;
            case 2: return xmlHttpRequest.responseText; break;
            case 3: return xmlHttpRequest.responseXML; break;
            default: return false;
        }
    }
}

/*
*post中文資料時使用,get時不需要使用
*/
function getEncodeURIComponent(arguments) {
    return encodeURIComponent(arguments);
}

/*
*使用例子
*alert(postData('t.jsp','name='+getEncodeURIComponent('深深深'),false,2,null));
*alert(getData('t.jsp','name=大大的',false,2,null));
*postData('t.jsp','name='+getEncodeURIComponent('深深深'),true,2,function (obj){alert(obj);});
*getData('t.jsp','name=大大的',true,2,function (obj){alert(obj);});
*/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.