客户端:
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
function startRequest(){
var text=document.getElementById("msgText").value;
createXMLHttpRequest();
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert(xmlHttp.responseText);
}
}
xmlHttp.open("POST","sendMsg.do",true);
xmlHttp.send("msgText="+text);
}
</script>
</head>
<body>
<input type="text" name="msgText" />
<input type="button" value="Send" onclick="startRequest()"/>
</body>
</html>
服务器端:String msg=request.getParameter("msgText");
问题:
为什么我得到的msg的值是null,而不是我在页面上输入的值?我该怎么获取页面上输入的值?
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
function startRequest(){
var text=document.getElementById("msgText").value;
createXMLHttpRequest();
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert(xmlHttp.responseText);
}
}
xmlHttp.open("POST","sendMsg.do",true);
xmlHttp.send("msgText="+text);
}
</script>
</head>
<body>
<input type="text" name="msgText" />
<input type="button" value="Send" onclick="startRequest()"/>
</body>
</html>
服务器端:String msg=request.getParameter("msgText");
问题:
为什么我得到的msg的值是null,而不是我在页面上输入的值?我该怎么获取页面上输入的值?
xmlHttp.setRequestHeader(”Content-Type”,”application/x-www-form-urlencoded”)