<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function btnClick(){
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//创建XMLHTTP对象,相当于WebClient
if (!xmlhttp) {
alert("创建xmlhttp对象异常!");
return false;
}
xmlhttp.open("POST", "ajax.ashx?ts"+new Date(), false); //准备向服务器的ajax.ash发出post请求
//XMLHTTP默认(也推荐)不是同步请求的,也就是open方法并不像WebClient的DownloadString那样把服务器返回的数据
//拿到才返回,数据异步的,因此需要监听onreadystatechange事件
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert(xmlhttp.responseText);
document.getElementById("Text1").value = xmlhttp.responseText;
}
else {
alert("AJAX服务器返回错误!");
}
}
}
xmlhttp.send();//这才开始发送请求
}
</script>
</head>
<body>
<input id="Text1" type="text" />
<input id="Button1" type="button" value="button" onclick="btnClick()"/>
</body>
</html>
<head>
<title></title>
<script type="text/javascript">
function btnClick(){
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//创建XMLHTTP对象,相当于WebClient
if (!xmlhttp) {
alert("创建xmlhttp对象异常!");
return false;
}
xmlhttp.open("POST", "ajax.ashx?ts"+new Date(), false); //准备向服务器的ajax.ash发出post请求
//XMLHTTP默认(也推荐)不是同步请求的,也就是open方法并不像WebClient的DownloadString那样把服务器返回的数据
//拿到才返回,数据异步的,因此需要监听onreadystatechange事件
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert(xmlhttp.responseText);
document.getElementById("Text1").value = xmlhttp.responseText;
}
else {
alert("AJAX服务器返回错误!");
}
}
}
xmlhttp.send();//这才开始发送请求
}
</script>
</head>
<body>
<input id="Text1" type="text" />
<input id="Button1" type="button" value="button" onclick="btnClick()"/>
</body>
</html>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货