js
var xmlHttp; // ajax if (window.XMLHttpRequest) {// code for IE7+,Firefox,Chrome,Opera,Safari xmlHttp = new XMLHttpRequest(); } else {// code for IE6,IE5 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) {// 4 = loaded if (xmlHttp.status == 200) {// 200 = "OK" var retstr = xmlHttp.responseText; var xmlDoc = new ActiveXObject("Microsoft.XmlDom"); xmlDoc.loadXML(retstr); var result = xmlDoc.getElementsByTagName("result")[0].firstChild.nodeValue; alert(result); } else { alert(xmlHttp.status); alert("出现错误,操作失败!"); } } } var url = "header.do"; xmlHttp.open("POST",url,true); xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlHttp.send("method=citeOneHeader&headerId=" + headerId + "&str=" + str);
action
response.setCharacterEncoding("utf-8"); response.setContentType("txet/xml;charset=utf-8"); response.setHeader("Cache-Control","no-cache"); response.getWriter().write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); StringBuffer sb=new StringBuffer(); sb.append("<res>"); sb.append("<result>"+result+"</result>"); sb.append("</res>"); sendResponse(response,sb.toString());
private void sendResponse(HttpServletResponse response,String responseText) throws IOException { response.getWriter().write(responseText); }原文链接:/ajax/165569.html