AJAX调用JAVA简单操作

调用的JS
//Ajax
function ajaxTool(url){
	xhr = createXml();
	xhr.open("post",url,false);
	xhr.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
	xhr.send(null);
	if(xhr.readyState==4&&xhr.status==200){
		return xhr.responseText;
	}
}

// 创建Ajax对象
function createXml(){
	if(window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		return new XMLHttpRequest;
	}
}

//执行调用
function callResult(){
	var a = ajaxTool("test_test.do?param1=value1");
	alert(a);
}



JAVA代码(struts2中)
/**
	 * AJAXTOOL
	 * 
	 * @author HeCheng
	 * @time 2010-01-20 10:20:05
	 * @param mes
	 */
	public void ajaxTool(String mes) {
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.setContentType("text/html;charset=UTF-8");
			response.setHeader("Pragma", "no-cache"); // HTTP 1.0
			response.setDateHeader("Expires", 0); // prevents caching at the
			// proxy server
			PrintWriter out = response.getWriter();
			out.print(mes);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
/**
	 * 被调用的方法
	 */
	public void test(){
		this.ajaxTool("ok");
	}

你可能感兴趣的:(java,html,Ajax,cache,Microsoft)