code289.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ajax中文乱码问题,另一种解决方案</title>
<script type="text/javascript">
String.prototype.lenB = function()
{
return this.replace(/[^\x0-\xf]/g,"##").length;
}
function getXMLHTTPRequest()
{
var myRequest = null;
if (window.XMLHttpRequest)
{
myRequest = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
myRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
return myRequest;
}
function send()
{
var name = document.getElementById("name");
try
{
var myRequest = getXMLHTTPRequest();
if(myRequest)
{
myRequest.onreadystatechange = function()
{
if(myRequest.readyState == 4)
{
window.alert(myRequest.responseText);
}
};
myRequest.open("POST","/test02/page10/pack07.code290",true);
//var msg = "name=" + name.value;
var msg = encodeURI(encodeURI("name=" + name.value));
myRequest.setRequestHeader("Content-Length",msg.lenB());
myRequest.setRequestHeader("content-type","application/x-www-form-urlencoded");
myRequest.send(msg);
}
}
catch(e)
{
window.alert(e);
}
}
</script>
</head>
<body>
<input type="text" id="name" name="name" value="飞翔"><br>
<input type="button" value="提交" onclick="send()">
</body>
</html>