六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 30|回复: 0

ajax实例

[复制链接]

升级  21.33%

20

主题

20

主题

20

主题

秀才

Rank: 2

积分
82
 楼主| 发表于 2013-1-23 02:28:17 | 显示全部楼层 |阅读模式
<script>var XmlHttpRequest;function callAjax(){document.getElementById('tips').innerHTML='正在合成......';                                    //创建ajax核心 xmlHttpRequest          XmlHttpRequest = false;           //下面需要建立一个XMLHttpRequest对象,用它进行服务器请求,针cf 不同浏览器建立方法不同           if (window.XMLHttpRequest)           { // Mozilla, Safari,...               XmlHttpRequest = new XMLHttpRequest();               if (XmlHttpRequest.overrideMimeType)               {                   XmlHttpRequest.overrideMimeType('text/xml');               }           }           else if (window.ActiveXObject)           { // IE               try             {                   XmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");               }               catch (e)               {                   try                  {                       XmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");                   }                   catch (e) {}               }           }                     if (!XmlHttpRequest) {               alert('出现错误,不能建立一个XMLHTTP实例!');               return false;           }         var content=document.getElementById('content').value;//var isIE= document.all ? true : false;//告诉sevlet是不是IE 浏览器var speed=document.getElementById('speakingRate').value;var postStr ="content="+content+"&speed="+speed;XmlHttpRequest.onreadystatechange=callBack;//设置回调的js函数  就是说发送请求 服务器响应后 回来执行的js函数   callBack是函数名XmlHttpRequest.open("POST","../experience",true);//第一个参数是请求类型(GET/POST) 第二个请求服务器路径 你可以写一个servlet地址 XmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   XmlHttpRequest.send(postStr);//开始向服务器发送xmlHttpRequest}//<!--回调的方法-->function callBack(){if(XmlHttpRequest.readyState==4){if(XmlHttpRequest.status==200)//以上两个判断 确定ajax请求已被成功相应{ var result = XmlHttpRequest.responseText;   //取出相应信息 /* 然后在这里执行相应后的信息,比如直接提示服务器相应的信息*/ if(document.getElementById('accplayer'))document.getElementById('accplayer').src=result; if(document.getElementById('iframe_pynon_0'))document.getElementById('iframe_pynon_0').src=result;}}}</script> 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表