日期:2014-05-16  浏览次数:20769 次

请高手指点 - Web 开发 / Ajax
哪位高手能给一个最简单的AJAX的例子

------解决方案--------------------
CSS code

html:
<label for="tbName">Your Name:</label><input id="tbName" name="tbName" type="text" />
<input id="btnSayHello" type="button" value="Say Hello From Server!" />
<div id="lbMessage"></div>

servlet:
doPost(request, response) {
String name = request.getParmeter("tbName");
response.getWriter().print(name);
}
JS:
function print() {
var name = document.getElementById("tbName").value;
sendRequest("url/servlet?name=" + name, lbMessage);
}
function sendRequest(url,showAreaId)
{
  var xmlHttp = new XMLHttpRequest();
  url += "&sessionId="+parseInt(Math.random()*(10000000));
  //alert(url);
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState==4)
    {
      if(xmlHttp.status==200){
      document.getElementById(showAreaId).innerHTML=xmlHttp.responseText;
    }
    else
    {
      alert('消息:'+xmlHttp.responseText);
      return;
    }
  }
 }

  xmlHttp.open("GET",url, false);
  xmlHttp.send(null);
}