日期:2014-05-16 浏览次数:20733 次
function forAjax() { alert("js forAjax"); // var url = '/wicketHello/tempFile/ajax01.txt'; // this path will be related the "servlet" var url = '/wicketHello/ajaxtest?flg=1&word=fuck'; xmlhttp = null; if (window.XMLHttpRequest) {// code for IE7, Firefox, Opera, etc. xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp != null) { [b]xmlhttp.onreadystatechange = state_Change; xmlhttp.open("GET", url, true); xmlhttp.send(null);[/b] } else { alert("Your browser does not support XMLHTTP."); } } function state_Change() { if (xmlhttp.readyState == 4) {// 4 = "loaded" if (xmlhttp.status == 200) {// 200 = "OK" //document.getElementById('ajaxMsg').innerHTML = xmlhttp.statusText; document.getElementById('divAjaxMsg').innerHTML = xmlhttp.responseText; } else { alert("Problem retrieving XML data:" + xmlhttp.statusText); } } }
<div id="divAjax"> <input type="button" value="Ajax Test" onclick="forAjax()"/> <span wicket:id="ajaxMsg">Message goes here</span> <br /> gogogogog <div id="divAjaxMsg"/> </div>
String par1 = request.getParameter("flg"); String par2 = request.getParameter("word"); // it is nessary to use "printWriter" to output the // infomation(String, json, xml...) PrintWriter out = response.getWriter(); out.println("these words come from servlet."+ " par1: " + par1 + " par2:" + par2); out.flush(); out.close();
<servlet-mapping> <servlet-name>ajax</servlet-name> <url-pattern>/ajaxtest</url-pattern> </servlet-mapping>