利用Ajax实现在脚本里传值实例介绍
时间:2020-11-02 17:41:56|栏目:JavaScript代码|点击: 次
页面脚本:
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;
复制代码 代码如下:
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;
栏 目:JavaScript代码
下一篇:javascript日期对象格式化为字符串的实现方法
本文标题:利用Ajax实现在脚本里传值实例介绍
本文地址:http://www.codeinn.net/misctech/18480.html