AJAX.htm的文件如下<!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","GetDate1.ashx", false);//准备向服务器的GetDate。ashx发出Post请求
//XMLHTTP默认不是同步请求的,也就是open方法并不像WebClient的DownloadString那样吧服务器返回的数据拿到了才返回,因此需要监听onreadystatechange事件
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4){
if (xmlhttp.status == 200){ //如果状态码为200则是成功
document.getElementById("Text1").value = xmlhttp.responseText; //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>
GetDate1.ashx的文件<%@ WebHandler Language="C#" Class="GetDate1" %>using System;
using System.Web;public class GetDate1 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write(DateTime.Now.ToString());
}
public bool IsReusable {
get {
return false;
}
}}
请高手指教一下 这段代码是我安装传智播客c#中得【传智播客.Net培训—ajax】1AJAX入门自己手打的
不过步骤都是按照他来的 很多的错误 是我们的编译环境不同还是 什么
我的编译环境是window2003(以安装iis) + vs2005 请高手指教
<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","GetDate1.ashx", false);//准备向服务器的GetDate。ashx发出Post请求
//XMLHTTP默认不是同步请求的,也就是open方法并不像WebClient的DownloadString那样吧服务器返回的数据拿到了才返回,因此需要监听onreadystatechange事件
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4){
if (xmlhttp.status == 200){ //如果状态码为200则是成功
document.getElementById("Text1").value = xmlhttp.responseText; //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>
GetDate1.ashx的文件<%@ WebHandler Language="C#" Class="GetDate1" %>using System;
using System.Web;public class GetDate1 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write(DateTime.Now.ToString());
}
public bool IsReusable {
get {
return false;
}
}}
请高手指教一下 这段代码是我安装传智播客c#中得【传智播客.Net培训—ajax】1AJAX入门自己手打的
不过步骤都是按照他来的 很多的错误 是我们的编译环境不同还是 什么
我的编译环境是window2003(以安装iis) + vs2005 请高手指教
//位置放错了
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4){
if (xmlhttp.status == 200){ //如果状态码为200则是成功
document.getElementById("Text1").value = xmlhttp.responseText; //responseText属性为服务器返回的文本
}
else
alert("AJAX服务器返回错误!");
}
};
xmlhttp.send();//这时才开始发送请求 xmlhttp.send();放到大括号外。
这个放在
xmlhttp.open("POST","GetDate1.ashx", false);的后面也行为了避免缓存的话你可以这样
xmlhttp.open("POST","GetDate1.ashx?rd="+Math.radom, false);//准备向服务器的GetDate。ashx发出Post请求