高分求ajax异步获得数据的程序!
当在jsp页面选择你需要的信息id时,此时ajax把id传到action中并在数据库中读取该条记录的所有信息,并且传回到jsp页面显示出来(id为该条信息在数据库中的住键)!问题解决一定给分,谢谢!
[解决办法]
你不可能要所以源代码吧?
我最多给你ajax的页面代码,这个是基于prototype框架的
首先你需要找个开源的prototype框架
<script type="text/javascript" src="../js/prototype/prototype.js"></script>
new Ajax.Updater('content',
url,//此url中可传你的id参数
{method:'get',
evalScripts:true,
onFailure:function(){programing = false;},
onSuccess:function(){programing = false;}
});
<div id='content'></div>
返回的结果是把你的'content'中的内容全部替换成jsp,你也可以在后台直接out.println();
这样就把你后台的数据直接替换到'content'中了
[解决办法]
你的url可以自己构建成类试xxx.do?id=1&other=other的形式
这样后台可以取到id了吧?
然后再根据id 处理数据就是了!
你要没那么懒的话,我想后台怎么做应该知道的吧?
不知道的话那你也就实在没做的必要了
[解决办法]
帮你做了个例子,一个html,有个服务器端的servlet,一个web.xml
将代码贴出来,你组装起来就可以了,要是不行,留个邮箱,我直接发过去给你
index.html
<html><head><title>ajax 异步取数据例子</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript"><!--var req;//ajax入口函数function getResult() { //这里ajaxGetData是application的名字 var url = "/ajaxGetData/servlet/GetInfoById?id="+escape(document.getElementById('txtID').value); createXMLHttp(); if(req){ req.open("GET",url, true); req.onreadystatechange = complete; req.send(null); } } //建立xmlhttpRequest对象 function createXMLHttp() { if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHttp"); } else if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } }//从服务器取回数据后怎么处理 function complete(){ if (req.readyState == 4) { if (req.status == 200) { document.getElementById('message').innerHTML = req.responseXML.getElementsByTagName("strcontent")[0].firstChild.data; } } }//--></script></head><body> <input type="text" id="txtID" value="" /> <input type="button" id="ajaxButton" value="点击我从服务器取数据" onclick="getResult();"/> <div id = "message"> 这是显示取回信息的地方 </div></body></html>
[解决办法]
<html><head><title>ajax 异步取数据例子</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript"><!--var xmlHttp; //声明xmlhttpRequest对象function createXMLHttpRequest() //建立xmlhttpRequest对象{ if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); }}function startRequest(url) //发送xmlhttpRequest请求{ createXMLHttpRequest(); xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET", url, true); xmlHttp.send(null);}function handleStateChange() //等待返回成功则处理{ if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { handleResult(); } }}function handleResult() //从服务器取回数据后怎么处理{ parent.document.getElementById("yourid").innerHTML = xmlHttp.responseText;}function getResult(){ index = modified_inbox_list_index; startRequest('your_action.do?id=' + document.getElementById('txtID').value);}//--></script></head><body> <input type="text" id="txtID" value="" /> <input type="button" id="ajaxButton" value="点击我从服务器取数据" onclick="getResult();"/> <div id = "yourid"> 这是显示取回信息的地方 </div></body></html>