httpPost(sURL, sParams)——使用该方法可以提交Post请求,一般情况下和addPostParam(sParams, sParamName, sParamValue)一起使用。如下举例:
例:
var sParams = "";
sParams = addPostParam(sParams, "name", "zhangyin");
sParams = addPostParam(sParams, "age", "26");
var sData = httpPost("http://www.gootrip.com/search.jsp", sParams);
addPostParam(sParams, sParamName, sParamValue)——增加Post请求时的参数。
addURLParam(sURL, sParamName, sParamValue) ——增加URL地址参数。
httpGet(sURL) ——使用该方法可以提交Get请求,一般情况下和addGetParam(sURL, sParamName, sParamValue)一起使用。
例:
var sURL = "";
sURL = addURLParam("http://www.gootrip.com/search.jsp", "name", "zhangyin");
var sData = httpGet(sURL);
Http.get(sURL, fnCallback) ——通过判断XMLHttpRequest的类型,以及判断ActiveXObject是否有效来确定http请求及其传输方式(一般用作Ajax技术的异步传输过程)。
例:
function getServerInfo() {//Post方法
var sURL = "http://www.gootrip.com/search.jsp ";
sURL = addGetParam(sURL, "name", "zhangyin");
sURL = addPostParam(sURL, "age", "26");
Http.get(sURL, function (sData) {// sData为Http.get返回数据
alert("Data from server: " + sData);
});
}
Http.post(sURL, sParams, fnCallback) ——通过判断XMLHttpRequest的类型,以及判断ActiveXObject是否有效来确定http请求及其传输方式(一般用作Ajax技术的异步传输过程)。
例:
function getServerInfo() {//Post方法
var sURL = "http://localhost/reflectpost.php";
var sParams = "";
sParams = addPostParam(sParams, "name", "Nicholas");
sParams = addPostParam(sParams, "book", "Professional JavaScript");
Http.post(sURL, sParams, function (sData) {
alert("Data from server: " + sData);
});
}
在http.js中的方法主要是对iClass.js中的ajax方法做的补充。
Code
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.");
}
};