利用Ajax实现在脚本里传值实例介绍
程序员文章站
2024-01-13 22:54:40
页面脚本: 复制代码 代码如下: function ajaxsave(urls) { //定义一个变量用于存放xmlhttprequest对象 var xmlhttp; /...
页面脚本:
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;
上一篇: 标签--格式化标签