下面是一个AJAX实例:
// JavaScript Document
var xmlHttp;
function GetXmlHttpObject()
{
var xhr=null;
try
{
// Firefox, Opera 8.0+, Safari
xhr=new XMLHttpRequest();
}catch (e)
{
// Internet Explorer
try
{
xhr=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e)
{
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xhr;
}
function getDataByDept(str)
{
xmlHttp = GetXmlHttpObject();
if(xmlHttp==null)
{
alert("很抱歉!您的浏览器不支持AJAX!");
return;
}
var url = "/intcard/jsp/employee.do?method=getByDept"
xmlHttp.onReadyStateChange=stateChanged;
xmlHttp.open("post",url,true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
xmlHttp.send("dept=" + str);
}
function stateChanged()
{
if(xmlHttp.readyState == 4)
{
var result = xmlHttp.responseText;
document.getElementById("select_employees").innerHTML = result;
}
}
思想就是,通过HTML控件的事件调用JS的方法,在JS理通过httprequest对象的open方法调用服务器脚本----- 在服务器脚本里 你可以用javabean的方法,并且把计算结果传递给JS.
参考:http://www.xue163.com/19990/6627/66271414.html