首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

ajax传递中文参数,乱码的解决方法

2012-12-26 
ajax传递中文参数,乱码的解决办法在前台页面编码两次:?urlencodeURI(url)?urlencodeURI(url)?举个例子

ajax传递中文参数,乱码的解决办法

在前台页面编码两次:

?url=encodeURI(url);
?url=encodeURI(url);

?

举个例子

usid为要传递到servlet的中文参数
var usid = encodeURI(sid);//uri编码

usid=encodeURI(usid);//再次uri编码
var url = "selectNameServlet?sid=" +usid;//合并构造url
if (window.XMLHttpRequest) {
?? ? ?req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
?? ? ?req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (req) {
?? ? req.open("GET", url, true);
?? ? req.onreadystatechange = complete;
?? ? req.send(null);
}
else
{
?? ? alert("fail to create request");
}
}

在服务器端对中文参数解码:

String sid = request.getParameter("sid");

String usid = URLDecoder.decode(sid, "utf-8");

?

热点排行