经过escape(javascript)编码的数据,只要含有汉字那么jsp页面里接收就为空null
客户端代码:
<body>
<form method= "POST " name= "form1 " action= "test2.jsp ">
<p> <input type= "text " name= "p1 " size= "20 "> <input type= "button " value= "按钮 " name= "B1 "> <input type= "reset " value= "全部重写 " name= "B2 "> </p>
</form>
</body>
<script language= "JavaScript ">
function form1.B1.onclick()
{
do_post();
}
function getXMLHttpRequest()
{
if(window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
request = new ActiveXObject( "Microsoft.XMLHTTP ");
if(!request)
{
request = new ActiveXObject( "Msxml2.XMLHTTP ");
}
return request;
}
}
function do_post()
{
var xmlhttprequest=getXMLHttpRequest();
if(xmlhttprequest)
{
var param= " ";
param=param+ "p2= "+escape(form1.p1.value);
alert(param);
xmlhttprequest.open( "POST ", "test2.jsp ",true);
xmlhttprequest.onreadystatechange = gotresult;
xmlhttprequest.setrequestheader( "content-length ",param.length);
xmlhttprequest.setRequestHeader( 'Content-Type ', 'application/x-www-form-urlencoded ');
xmlhttprequest.send(param);
}
function gotresult()
{
if (xmlhttprequest.readyState == 4)
{
if(xmlhttprequest.status == 200 )
{
//prompt( ' ',xmlhttprequest.responseText);
}
}
}
}
服务器端:
<%
try
{
String t_p2=request.getParameter( "p2 ");
System.out.print(t_p2);
}
catch(Exception e)
{
e.printStackTrace();
}
%>
只要含有汉字,在控制台就显示null
[解决办法]
param=param+ "p2= "+encodeURI(form1.p1.value);
这样可以不? 服务器端我就设了一下request的characterEncoding为utf-8