代码如下:
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("div").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", "/registeServlet", true);
xmlhttp.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xmlhttp.send("firstName=" + $("#FirstName").value & "lastName="
+ $("#LastName"));
}
</script>
</head>
<body>
<form>
First name: <input type="text" name="FirstName" value="Bill"
id="FirstName" /><br /> Last name: <input type="text"
name="LastName" value="Gates" id="LastName" /><br />
<!-- <button type="submit">提交<tton> -->
<button type="button" onclick="loadXMLDoc()">序列化表单值<tton>
</form>
<div id="results"></div>
为什么我的post就是传不到servlet呢?URL地址我已经验证过在浏览器可以访问到,如果直接用表单做也可以提交 但是这样就是不行 急啊。。。各位大神帮帮忙啊
encodeURIComponent编码下内容,可能是出乱码了
xmlhttp.send("firstName=" + encodeURIComponent$("#FirstName").value) & "lastName="
+ encodeURIComponent$("#LastName")));
而且你倒入了jquery框架,干嘛还要自己写ajax代码。。⊙﹏⊙b汗。。。
而且方法也用错了。。jquery对象没有value属性。一个传递的是jquery对象。字符串连接也错了,是+,&要放到引号里面
xmlhttp.send("firstName=" + encodeURIComponent($("#FirstName").val()) + "&lastName=" + encodeURIComponent($("#LastName").val()));