javascript和JQuery焦点图和代码特效大全
当前最流行的开源CMS网站系统大全
当前位置:首页 > WEB前端 > AJAX

Java程序与Ajax使用实例代码

来源:IT技术网编辑:雨天发布于:2017-05-01人围观

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> ajax test </TITLE>

 </HEAD>
<script src="http://blog.sina.com.cn/s/ajaxaction.js"></script>
 <BODY>
 <form>
 <input type="text" name="content">
 <input type="button" name="clickmeforget" value="clickme" >
 <input type="button" name="clickmeforpost" value="clickme" >
 </form>
  <span id="textHit"></span>
  <script>
  function testPostAction(url,value,stateChanged){
  value = encodeURI(value);
  value = encodeURI(value);//两次,很关键,解决中文问题

  sendPostAction(url,"content="+value,stateChanged);
  };
  function stateChanged(){
  if (xmlHttp.readyState==4)
  {
  document.getElementByIdx("textHit").innerHTML=xmlHttp.responseText;
  };
  }
  </script>
 </BODY>
</HTML>

#test.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%
String content = request.getParameter("content");
out.println(java.net.URLDecoder.decode(content,"UTF-8"));
out.println("中文");
%>

#ajaxaction.js

//定义全局的xml对象
var xmlHttp = null;
function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
 if(xmlHttp == null)
  alert("对不起,您的浏览器不支持Ajax");
  return xmlHttp;
}

function sendGetAction(url,myStateChanged){
  //get传送
 
 if (xmlHttp==null)
  {
   xmlHttp=GetXmlHttpObject();

  }
 
  //指定响应函数
  xmlHttp.onreadystatechange=myStateChanged;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function getQuery(form){
//将表单元数拼成Query条件
    queryString="";
   
 
    var numberElements =  form.elements.length;
    for(var i = 0; i < numberElements; i++) {
        if(i < numberElements-1) {
            queryString += form.elements[i].name+"="+
                           encodeURI(encodeURI(form.elements[i].value))+"&";
        } else {
            queryString += form.elements[i].name+"="+
                           encodeURI(encodeURI(form.elements[i].value));
        }

    }
 return queryString;
   
}

function sendGetForm(form,myStateChanged){
  //表单GET提交
  var queryString = getQuery(form);
  var url = form.action;
  url = url+"?"+queryString+"&sid="+Math.random();
  sendGetAction(url,myStateChanged);
 
}

function sendPostAction(url,strQuery,myStateChanged){
//post传送

   if (xmlHttp==null)
  {
  xmlHttp=GetXmlHttpObject();
 
  }
  xmlHttp.open("POST",url,true);
  xmlHttp.onreadystatechange=myStateChanged;
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.send(strQuery);//发送请求
};

function sendPostForm(form,myStateChanged){
//表单post提交
  var queryString = getQuery(form);
  var url = form.action;
  sendPostAction(url,queryString,myStateChanged);

}

与相关的文章
有时间的话来看看IT界的突发事件