作者:红星闪闪的电灯泡 | 来源:互联网 | 2022-03-06 14:49
页面脚本:
代码如下:
function ajaxSave(URLS) {
//定义一个变量用于存放XMLHttpRequest对象
var xmlhttp;
//定义一个变量用于存放 从服务器返回的响应结果
var respOnseContext= "";
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 && xmlhttp.status == 200) {
respOnseContext= xmlhttp.responseText;
alert(responseContext);
}
}
xmlhttp.open("POST", URLS, true);
xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlhttp.send();
}
后台方法:
代码如下:
Boolean boolean1;
String repOnseText= "";
if(boolean1){
repOnseText="保存成功!";
}
else{
repOnseText="保存失败!";
}
HttpServletResponse respOnse= ServletActionContext.getResponse();
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(reponseText);
out.flush();
out.close();
return null;