AJAX同步提交和异步提交方法
来源:互联网 发布:windows embedded 编辑:程序博客网 时间:2024/04/30 12:10
AJAX提交有同步提交和异步提交两种方式。后一种方式为提交后不等服务器有响应继续执行客户端代码。可以利用异步提交显示一些界面效果,比如显示一些文字,动画等。如下代码供大家参考。
//提交客户端DataStore数据
//url--目的URL
//data--要提交的DataStore 对象或XML字符串
//action--页面的Action代码
//isAsync-true: Async mode;false: sync mode
function Execute(url,action,data,method,isAsync){
var acturl = url+"";
var hrf = document.createElement("<A href='"+url+"'>");//URL格式化
var tg = "DataStore";
document.appendChild(hrf);
acturl= hrf.href;
if(url == "#")
{
acturl = acturl.replace("#","");
var pos = acturl.indexOf("?");
if(pos>0)acturl = acturl.substr(0,pos);
}
if (acturl.indexOf("?") > 0)
acturl = acturl + "&IsExec=True&RequestAction=" + action;
else
acturl = acturl + "?IsExec=True&RequestAction=" + action;
var _data = "";
var execdata = "";
if(typeof(data) == "object")
_data = data.ToString();
else if(typeof(data) == "string")
_data = data;
if(!isBase64Encode)
{
execdata = _data.replace(//xB7/g,"%B7");//由于服务器端Request处理中点,解码时会变成问号
execdata = escape(_data).replace(//+/g,"%2B")//由于服务器端Request处理"+"解码时会变成空格
}
else
{
tg = "DataStoreBase64";
execdata = _Encode64(_data);
}
var resdata;
if(!method)method = "post";
if(method.toLowerCase() == "get"){
if(typeof(isAsync) == "undefined" || isAsync == false)
resdata = GetRequest(acturl + "&" + tg + "=" + execdata,isAsync);
else
GetRequest(acturl + "&" + tg + "=" + execdata,isAsync);
}
else if(method.toLowerCase() == "post"){
if(typeof(isAsync) == "undefined" || isAsync == false)
resdata = PostRequest(acturl,tg + "=" + execdata,isAsync);
else
PostRequest(acturl,tg + "=" + execdata,isAsync);
}
if(typeof(isAsync) == "undefined" || isAsync == false)
return new ExecResult(resdata,acturl,action,_data,method);
}
var xmlhttp = null;
function GetRequest(url,isAsync)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
if(typeof(isAsync) == "undefined" || isAsync == false){
xmlhttp.Open("GET",url,false);
}
else{
xmlhttp.Open("GET",url,true);
xmlhttp.onreadystatechange = HandleStateChange;
}
xmlhttp.send();
if(typeof(isAsync) == "undefined" || isAsync == false)
return xmlhttp.responseText;
}
function PostRequest(url,data,isAsync)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
if(typeof(isAsync) == "undefined" || isAsync == false)
xmlhttp.Open("POST",url,false);
else{
xmlhttp.Open("POST",url,true);
xmlhttp.onreadystatechange = HandleStateChange;
}
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(data);
if(typeof(isAsync) == "undefined" || isAsync == false)
return xmlhttp.responseText;
}
//Add by Max Yu 2006-11-18
function HandleStateChange(){
if (xmlhttp.readyState == 4){
if(xmlhttp.responseText.indexOf("<DataStore") != 0)
{
var win = window.open("about:blank","_Error");
win.document.write(xmlhttp.responseText);
return;
}
if (typeof(ResDataLoaded) == "function")ResponseDataLoaded = ResDataLoaded;
if (ResponseDataLoaded != null)
ResponseDataLoaded(new DataStore(xmlhttp.responseText));
}
}
- AJAX同步提交和异步提交方法
- AJAX同步提交和异步提交方法
- Ajax 异步提交方法
- form表单同步提交和异步提交
- form表单同步提交和异步提交
- ajax异步与同步提交注意事项
- ajax提交的同步与异步
- ajax提交异步,同步请求不同
- Ajax异步提交表单数据简单方法
- Ajax异步提交表单数据简单方法
- Thinkphp ajax异步提交的方法总结
- Ajaxpro2 异步提取数据 Ajax异步方法 异步提交
- ajax异步、同步问题,KindEditor ajax提交内容,ajax提交form表单
- ajax 表单提交和异步请求
- AJAX的异步提交
- ajax异步提交
- ajax异步提交form
- jquery ajax异步提交
- 很简单很快速的企业信息化系统
- 线上出版社的一些想法
- Fedora 10 安装VMWare-Tools启动不了解决方法
- 工业相机与普通相机的区别
- j2me 开发平台的搭建
- AJAX同步提交和异步提交方法
- 条件编译、Unix中C函数声明、变量存储类型、程序与进程
- 碎碎念
- JAVA中转义字符
- 【转】事实胜于雄辩 央视及举报中心蓄意捏造
- “表单控件”与“实体类”
- 正面思考
- Thinkpad SL400安装xp系统
- 何获取机器的ip地址