/*在有中文參數時,接收方需要使用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);});
*/