当前位置: 编程技术>java/j2ee
JSP XMLHttpRequest动态无刷新及其中文乱码处理
来源: 互联网 发布时间:2014-10-18
本文导语: 1.在页面的内加入 //无刷新请求的xmlhttp请求 var http_request=false; function createRequest(url) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType('text/ht...
1.在页面的内加入
//无刷新请求的xmlhttp请求
var http_request=false;
function createRequest(url)
{
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('不能创建XMLHTTP实例!');
return false;
}
http_request.onreadystatechange=alertContents;
http_request.open("POST",url,true);
http_request.send(null);
}
2.调用--在js调用请求【CONTROL为servlet配置的url-mapping,传参跟一般url传参一样】
createRequest('CONTROL'+'?cmd=listen');
3.在servlet中处理【中文乱码问题】
PrintWriter out;
response.setContentType("html/text");//此5行必备,用于输出中文,否则乱码
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control","no-cache");
out=response.getWriter();
out.println("对不起,系统忙,请稍后再试!"); //为返回的响应结果,在页面直接以alert()显示
//无刷新请求的xmlhttp请求
var http_request=false;
function createRequest(url)
{
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('不能创建XMLHTTP实例!');
return false;
}
http_request.onreadystatechange=alertContents;
http_request.open("POST",url,true);
http_request.send(null);
}
2.调用--在js调用请求【CONTROL为servlet配置的url-mapping,传参跟一般url传参一样】
createRequest('CONTROL'+'?cmd=listen');
3.在servlet中处理【中文乱码问题】
PrintWriter out;
response.setContentType("html/text");//此5行必备,用于输出中文,否则乱码
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control","no-cache");
out=response.getWriter();
out.println("对不起,系统忙,请稍后再试!"); //为返回的响应结果,在页面直接以alert()显示