客户端到服务器通信的几个函数(JS)
时间:2010-08-28 来源:galaxylancer
/**********************************************************************
submit button的响应函数
**********************************************************************/
function ValidateCtrlAndSubmit(ctrlList,isAsync)
//check and submit
{
//change
if(g_lockLink)
//if this submit button is locked
{
WS("LOCKED..."); //LOCKED...
return;
}
if(isAsync==null)
isAsync=false; //is not asynchronous
var res=true;
for(var key in ctrlList)
{
var obj=ctrlList[key];
if(obj.active)
//if the object is active
{
res=obj.IsPass(); //the object is pass,the give value TURE to res
//just to check each object
if(!res)
{
return res;
}
}
}
if(MY_BEFORE_SUBMIT()==false)
//something should be checked before submit
{
return false;
}
//check-work ends
//send-work starts
g_httpOK=true;
var o=c_iniUrl; //the variable which would be sent
var len=g_MaxSubmitLen; //command length
for(var key in ctrlList)
//try to send the commands to the server
{
var obj=ctrlList[key];
if(obj.active)
//if this object is active
{
var str=GetSetterCmd(obj,GetCtrlValue(ctrlList,key)); //get command
len-=str.length; //the length lefted
o+=str; //command be added
if(len<10)
//the space almost full,so could be sent
{
SendHttp(o,isAsync); //the communication between both ends
if(g_httpOK)
//if sent successfully,then try to send the next command
{
o=c_iniUrl;
len=g_MaxSubmitLen;
}
else
{
break;
}
}
}
}
if(len!=g_MaxSubmitLen)
//send the commands remains
{
SendHttp(o,isAsync);
}
if(g_httpOK)
//sent successfully
{
MY_SUBMIT_OK();
}
}
/*******************************************************************************
客户端向服务器发送请求的函数
*******************************************************************************/
function SendHttp(url,isAsync,callBack)
{
isAsync=new Boolean(isAsync);
g_SubmitHttp=null;
g_SubmitHttp=InitXHttp(); //check the browser versions,e.g. Microsoft.XMLHTTP,
// and create the request
if(callBack!=null)
//set the processing function
{
g_SubmitHttp.onreadystatechange=callBack; //the processing function
}
else
{
g_SubmitHttp.onreadystatechange=OnSubmitReadyStateProcess; //the processing function
}
try
{
g_SubmitHttp.open("GET",url,isAsync); //open the url,and get the message
g_SubmitHttp.setRequestHeader("If-Modified-Since","0"); //set request header
g_SubmitHttp.send(null); //send the information to the server
WS(GL("sending_")); //waiting
}
catch(e)
{
};
}
/*************************************************************
检测浏览器,并创建请求
*************************************************************/
function InitXHttp()
{
var xhttp=null;
if(IsMozilla())
{
xhttp=new XMLHttpRequest();
if(xhttp.overrideMimeType)
{
xhttp.overrideMimeType('text/xml');
}
}
else
if(browser_IE)
{
try
{
xhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
};
}
}
return xhttp;
}
/***********************************************************************
对服务器响应的数据的处理
***********************************************************************/
function OnSubmitReadyStateProcess()
//processing function
{
if(g_SubmitHttp.readyState==4)
//if the server has responded
{
if(g_SubmitHttp.status!=200)
//if the server is not ready
{
alert(GL(err_submit_fail));
g_httpOK=false; //then we cann't send information
WS(GL(fail_));
}
else
//if the status be OK,then we could do our sending-work continually
{
g_httpOK=true;
WS(GL(ok_));
}
}
}