2014-12-07:ajax学习

来源:互联网 发布:股票分析算法方法 编辑:程序博客网 时间:2024/06/08 20:02

AJAX学习:

JSP: function ajax(){

   var xmlhttp;

   if(window.XMLHttpRequest){ //创建XMLHTTPrequest对象:ie5,ie6用Microsoft.XMLHTTP

      xmlhttp = new XMLHttpRequest();

   }else{

      ActiveObject("Microsoft.XMLHTTP");

  

   }

   xmlhttp.open("POST","basicinformation_zgxx_ajaxTest.action",true);//以post方式提交,路径,是否为异步

xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//post时:加HTTP头

   xmlhttp.send("gh=132&id=2");//传值仅用于post,get直接在路径上写

   xmlhttp.onreadystatechange=function(){//准备状态改变时

   if(xmlhttp.readyState==4&&xmlhttp.status==200)//表示已经响应就绪

      {

         alert(xmlhttp.responseText);//得到后台传的值

      }

   }

   }

后台:

public voidajaxTest(){

      System.out.println("+++"+gh);

      HttpServletResponsere =ServletActionContext.getResponse();

      re.setCharacterEncoding("UTF-8");//指定返回值的编码方式,必须放在out声明之前

      re.setContentType("text/html;charset=UTF-8");

      PrintWriterpw = null;

      Stringstr = "ajax传输";

      try {

         pw= re.getWriter();

         pw.append(str);

         pw.flush();

      }catch(IOException e) {

         e.printStackTrace();

      }finally{

         pw.close();

      }

     

     

     

   }

0 0
原创粉丝点击