当前位置: 代码迷 >> J2ME >> AJAX出现乱码了~解决方法
  详细解决方案

AJAX出现乱码了~解决方法

热度:8644   发布时间:2013-02-25 21:33:21.0
AJAX出现乱码了~
1、jsp 
  <script type="text/javascript">
var xmlHttp;
function creatXMLHttp() {
var username =document.getElementById("Username").value;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXobject("Microsoft.XMLHTTP");
}
}
function UpdateUser(username) {
creatXMLHttp();
xmlHttp.open("POST", "../UpdateSelectAdmin.asp?Username=" + username);
xmlhttp.setRequestHeader( "Content-Type", "text/html;charset=GBK" );
xmlHttp.onreadystatechange = UpdateUserCallback;
xmlHttp.send(null);
}
function UpdateUserCallback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var text = xmlHttp.responseText;
document.getElementById("txtHint").innerHTML = text;
}
}
}
</script>

out.print("<input type='hidden' name='Username' value="+ login.getUsername() + ">");

2、 response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username=request.getParameter("Username");
out.print(username);
找了很多案例都无法解决。求帮忙啊~

------解决方案--------------------------------------------------------
ajax里
username 是中文吗?
username=encodeURI(username);
username=encodeURI(username); //最重要的部分,两次调用encodeURI ,就是编码两次

servlet
String name=request.getParameter("username");
name=URLDecoder.decode(name,"utf8"); //post 传递的时候,一定是用utf8编码的,url 自己可以设定
response.setContentType("text/html;charset=utf-8")
------解决方案--------------------------------------------------------
JScript code
function onSendInfo(reqId){    if(confirm("是否确认发送站内短信?")){        var friendList = document.getElementById("friendList");        var msgContent = document.getElementById("textarea2");        if(onValidate(friendList,msgContent)){            return;        }        var usernames=onSelect(friendList);        var params="?reqId="+reqId                +"&msgContent="+encodeURI($("#textarea2").val())                +"&sendMember="+encodeURI(usernames)                +"&msgTitle="+encodeURI($("#textarea").val());        $.ajax({                 type: "post",                 url: "motorcade_alliance_send_message.jspx"+params,            dataType: "json",/*这句可用可不用,没有影响*/             contentType: "application/json; charset=utf-8",                 success: function (data) {                 if(data.status==0){                    alert('发送站内短信成功!');                }else if(data.status==1){                    alert(data.errors);                }            },              beforeSend: function(XMLHttpRequest){                   XMLHttpRequest.setRequestHeader("RequestType", "ajax");               },               error: function (XMLHttpRequest, textStatus, errorThrown) {                 alert(errorThrown);                 }             });    }}
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Thu Sep 20 17:19:56 CST 2018
  • 最近登录:Thu Sep 20 17:19:56 CST 2018
  • 最近登录:Thu Sep 20 17:19:56 CST 2018
  • 最近登录:Thu Sep 20 17:19:56 CST 2018
  • 最近登录:Thu Sep 20 17:19:56 CST 2018