跨浏览器通用XMLHttpRequest

http.js

 


var bXmlHttpSupport = (typeof XMLHttpRequest == "object" || window.ActiveXObject);

function httpPost(sURL, sParams) {
                      
    var oURL = new java.net.URL(sURL);
    var oConnection = oURL.openConnection();

    oConnection.setDoInput(true);
    oConnection.setDoOutput(true);
    oConnection.setUseCaches(false);               
    oConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");               

    var oOutput = new java.io.DataOutputStream(oConnection.getOutputStream());
    oOutput.writeBytes(sParams);
    oOutput.flush();
    oOutput.close();

    var sLine = "", sResponseText = "";

    var oInput = new java.io.DataInputStream(oConnection.getInputStream());                               
    sLine = oInput.readLine();
   
    while (sLine != null){                               
        sResponseText += sLine + "/n";
        sLine = oInput.readLine();
    }
                                 
    oInput.close();                                 

    return sResponseText;                        
}

function addPostParam(sParams, sParamName, sParamValue) {
    if (sParams.length > 0) {
        sParams += "&";
    }
    return sParams + encodeURIComponent(sParamName) + "="
                   + encodeURIComponent(sParamValue);
}

function addURLParam(sURL, sParamName, sParamValue) {
    sURL += (sURL.indexOf("?") == -1 ? "?" : "&");
    sURL += encodeURIComponent(sParamName) + "=" + encodeURIComponent(sParamValue);
    return sURL;  
}

function httpGet(sURL) {
    var sResponseText = "";
    var oURL = new java.net.URL(sURL);
    var oStream = oURL.openStream();
    var oReader = new java.io.BufferedReader(new java.io.InputStreamReader(oStream));
   
    var sLine = oReader.readLine();
    while (sLine != null) {
        sResponseText += sLine + "/n";
        sLine = oReader.readLine();
    }
   
    oReader.close();
    return sResponseText;
}

if (typeof XMLHttpRequest == "undefined" && window.ActiveXObject) {

    function XMLHttpRequest() {

        var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
                             "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
                             "Microsoft.XMLHTTP"];
                        
        for (var i=0; i < arrSignatures.length; i++) {
            try {
       
                var oRequest = new ActiveXObject(arrSignatures[i]);
           
                return oRequest;
       
            } catch (oError) {
                //ignore
            }
        }         

        throw new Error("MSXML is not installed on your system.");              
    }
}


var Http = new Object;

Http.get = function (sURL, fnCallback) {
 
    if (bXmlHttpSupport) {
  
        var oRequest = new XMLHttpRequest();
        oRequest.open("get", sURL, true);
        oRequest.onreadystatechange = function () {
            if (oRequest.readyState == 4) {
                fnCallback(oRequest.responseText);
            }
        }
        oRequest.send(null);   
   
    } else if (navigator.javaEnabled() && typeof java != "undefined"
            && typeof java.net != "undefined") {
           
        setTimeout(function () {
            fnCallback(httpGet(sURL));
        }, 10);
    } else {
        alert("Your browser doesn't support HTTP requests.");
    }         

};

Http.post = function (sURL, sParams, fnCallback) {
 
    if (bXmlHttpSupport) {
  
        var oRequest = new XMLHttpRequest();
        oRequest.open("post", sURL, true);
        oRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        oRequest.onreadystatechange = function () {
            if (oRequest.readyState == 4) {
                fnCallback(oRequest.responseText);
            }
        }
        oRequest.send(sParams);   
   
    } else if (navigator.javaEnabled() && typeof java != "undefined"
            && typeof java.net != "undefined") {
           
        setTimeout(function () {
            fnCallback(httpPost(sURL, sParams));
        }, 10);
    } else {
        alert("Your browser doesn't support HTTP requests.");
    }         

};

 

 

<html>
    <head>
        <title>Cross-Browser HTTP Request Example</title>
        <script type="text/javascript" src="http.js"></script>
        <script type="text/javascript">              
               
                function getServerInfo() {
                    var sURL = "example.txt";
                    Http.get(sURL, function (sData) {
                        alert("Data from server: " + sData);
                    });

                }

        </script>
    </head>
    <body>
        <p>Click the button to make a GET request. This example will only work
        on a Web server. You must replace <code>http://localhost/example.txt</code>
        with the appropriate location of a file on your server.</p>
       
        <input type="button" οnclick="getServerInfo()" value="Get Server info" />
    </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值