liuxiaozhao

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ajax作为前端技术,采用异步方式,根据其采用的方式来讲,不用刷新界面,只是进行数据的传递。

后台还是用servlet的。servlet接收到ajax的get或post请求后。将数据组装成xml或者json的方式返回到前端。

此时ajax获取到数据之后,进行解释展示即可。

 

//ajax 请求

function showInfo(){
   var xmlhttp;
   if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
   }else{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange=function(){
    //alert(xmlhttp.readyState);
    if(xmlhttp.readyState==4&&xmlhttp.status==200){
     var respText = xmlhttp.responseText;
     var jsonT = eval("("+respText+")");
     document.getElementById("info").innerHTML=jsonT.info;
    }
   }
   var name = document.getElementById("name").value;
   var url="../test/hello?t=" + Math.random();
   var sendStr = "name="+name;
   
   xmlhttp.open("POST",url, true);
   xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
   xmlhttp.send(sendStr);
}

posted on 2016-01-19 14:44  liuxiaozhao  阅读(123)  评论(0编辑  收藏  举报