asp 中 使用js 调用XMLHTTP乱码等问题
调用处
<td width= "5% " align= "center " bgcolor= "#0099FF " class= "font12_ffffff " style= "display:block;cursor:hand; " onClick= "s( <%=z_num%> , <%=gsid%> , <%=gname%> , <%=gyear%> , <%=gplace%> , <%=gid%> , <%=gnum%> ) "> <strong> S </strong> </td>
我传的参数中 gname 为字符,或为中文或为英文,英文或数字时返回乱码,为中文时 提示我 传入的值 “未定义”
以下是JS代码
var http=new ActiveXObject( "MSXML2.XMLHTTP.3.0 ")
var zt=0 // tr 的状态 0为隐藏 1为显示
var zt1=0 // tr 的状态 0为隐藏 1为显示
function s(z_num,gsid,gname,gyear,gplace,gid,gnum)
{
var url= "s.asp?z_num= "+z_num+ "&gsid= "+gsid+ "&gname= "+gname+ "&gyear= "+gyear+ "&gplace= "+gplace+ "&gid= "+gid+ "&gnum= "+gnum;
var tr= "tr "+z_num;
if (zt==0 || zt1==1)
{
http.open( "post ",url,false);
//http.setRequestHeader( "Content-Type ", "text/xml ");
http.setRequestHeader( "encoding ", "gb2312 ");
http.send();
if (http.readyState==4)
{
var td= "td "+z_num;
//alert(http.responseText)
document.getElementById(td).innerHTML=http.responseText;
}
document.getElementById(tr).style.display= " ";
zt=1;
}
else
{
document.getElementById(tr).style.display= "none ";
zt=0;
}
}
------解决方案--------------------s( <%=z_num%> , <%=gsid%> , <%=gname%> , <%=gyear%> , <%=gplace%> , <%=gid%> , <%=gnum%> )
如果传输过去的是变量是字符请加上‘’
------解决方案--------------------var url= "s.asp?z_num= "+escape(z_num)+ "&gsid= "+gsid+ "&gname= "+gname+ "&gyear= "+gyear+ "&gplace= "+gplace+ "&gid= "+gid+ "&gnum= "+gnum;
后面的自己加
document.getElementById(td).innerHTML=unescape(http.responseText);
------解决方案--------------------也想知道
------解决方案--------------------中文要用escape编码和unescape解码
英文不用,你可以自己判断下是中文还是英文,再决定是否编码
------解决方案--------------------在JS中加上一句:
xmlHttp.setRequestHeader( "CONTENT-TYPE ", "application/x-www-form-urlencoded ");
------解决方案--------------------XMLHTTP获取的目标网页编码为UTF-8时,获取到的源码中中文就不会是乱码了
------解决方案--------------------在你的asp 页 开始写 response.charset= gb2312
------解决方案--------------------