<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p><span id="A1"></span></p>
<button onclick="sendformdata()">发送数据</button>
<script type="text/javascript">
var xmlhttp;
function sendformdata()
{
if (window.XMLHttpRequest) { // Mozilla 浏览器 //新建XMLHttpRequest对象
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 浏览器
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { }
}
}
if (xmlhttp!=null)
{
var formData = new FormData();
formData.append('username','lee');
formData.append('usercity','beijing');
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open('POST','showinfo.asp');
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send(formData);
}
else
{
alert("您的浏览器不支持 XMLHTTP.");
}
}
function state_Change()
{
if (xmlhttp.readyState==4) // 服务器已经响应
{
if (xmlhttp.status==200) // 请求成功
{
// 显示服务器的响应数据
document.getElementById('A1').innerHTML=xmlhttp.responseText;
}
else
{
alert("接收XML数据时出现问题:" + xmlhttp.statusText);
}
}
}
</script>
</body>
</html>
后台showinfo.asp 接收数据并显示
showinfo.asp 的代码如下:
<%@LANGUAGE="VBSCRIPT" codepage="65001"%>
<%
username=request.form("username")
usercity=request.form("usercity")
response.write "名字:" & username&"<br>"
response.write "城市:" & username&"<br>"
%>
执行完第一个程序后,显示不出来传递的数据:lee 和Beijing
请教了,谢谢!!!
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
去掉这个
谢谢!
试了去掉也不行,还是显示不出数据:lee,beijing 这些值
你要是搞不明白原生的XMLHttpRequest的用法的话你就用jquery的ajax吧,比这方便多了,你就不用纠结前端的问题了
liurongmoli 我这是书上的例子,必须弄懂用FormData 发送数据,现在不明白的是后台要如何处理FormData 发送的数据。