如题,小弟不知哪里弄了出错了,请指教一下
前台页面如下
<%@ Page Language="C#" CodeFile="AjaxTest.aspx.cs" Inherits= "test_AjaxTest" %><!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 id="Head1" runat="server">
<title>测试</title>
<script language="javascript" type="text/javascript">
<!--
function GetInfo(){//我们就是通过这个函数来异步获取信息的
var xmlHttpReq = null;//声明一个空对象用来装入XMLHttpRequest
if (window.XMLHttpRequest){//除IE5 IE6 以外的浏览器XMLHttpRequest是window的子对象
xmlHttpReq = new XMLHttpRequest();//我们通常采用这种方式实例化一个XMLHttpRequest
}
else if (window.ActiveXObject){//IE5 IE6是以ActiveXObject的方式引入XMLHttpRequest的
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
//IE5 IE6是通过这种方式
}
if(xmlHttpReq != null){//如果对象实例化成功 我们就可以干活啦
xmlHttpReq.open("get","AjaxTest.aspx?s=1",true);
//调用open()方法并采用异步方式
xmlHttpReq.onreadystatechange=RequestCallBack; //设置回调函数
xmlHttpReq.send(null);//因为使用get方式提交,所以可以使用null参调用
}
function RequestCallBack(){//一旦readyState值改变,将会调用这个函数
if(xmlHttpReq.readyState == 4)
{
document.getElementByIdx_x("iptText").value = xmlHttpReq.responseText;
//将xmlHttpReq.responseText的值赋给iptText控件
}
}
}
-->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="iptText" type="text" value="" />
<input type="button" id="" value="Ajax提交" onclick="GetInfo();" />
<!--点击这个按钮调用-->
</div>
</form>
</body>
</html>
后台代码如下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class test_AjaxTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["s"] == "1")//使用查询字串来指示这个请求是通过Ajax发出的
{ Response.Write("hello world!");//向HttpResponse中输出hello world!
Response.End();//将页面缓冲发送向客户端浏览器 并中止该页输出
//如果去掉这句 会得到多余的HTML代码
} }
}
前台页面如下
<%@ Page Language="C#" CodeFile="AjaxTest.aspx.cs" Inherits= "test_AjaxTest" %><!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 id="Head1" runat="server">
<title>测试</title>
<script language="javascript" type="text/javascript">
<!--
function GetInfo(){//我们就是通过这个函数来异步获取信息的
var xmlHttpReq = null;//声明一个空对象用来装入XMLHttpRequest
if (window.XMLHttpRequest){//除IE5 IE6 以外的浏览器XMLHttpRequest是window的子对象
xmlHttpReq = new XMLHttpRequest();//我们通常采用这种方式实例化一个XMLHttpRequest
}
else if (window.ActiveXObject){//IE5 IE6是以ActiveXObject的方式引入XMLHttpRequest的
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
//IE5 IE6是通过这种方式
}
if(xmlHttpReq != null){//如果对象实例化成功 我们就可以干活啦
xmlHttpReq.open("get","AjaxTest.aspx?s=1",true);
//调用open()方法并采用异步方式
xmlHttpReq.onreadystatechange=RequestCallBack; //设置回调函数
xmlHttpReq.send(null);//因为使用get方式提交,所以可以使用null参调用
}
function RequestCallBack(){//一旦readyState值改变,将会调用这个函数
if(xmlHttpReq.readyState == 4)
{
document.getElementByIdx_x("iptText").value = xmlHttpReq.responseText;
//将xmlHttpReq.responseText的值赋给iptText控件
}
}
}
-->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="iptText" type="text" value="" />
<input type="button" id="" value="Ajax提交" onclick="GetInfo();" />
<!--点击这个按钮调用-->
</div>
</form>
</body>
</html>
后台代码如下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class test_AjaxTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["s"] == "1")//使用查询字串来指示这个请求是通过Ajax发出的
{ Response.Write("hello world!");//向HttpResponse中输出hello world!
Response.End();//将页面缓冲发送向客户端浏览器 并中止该页输出
//如果去掉这句 会得到多余的HTML代码
} }
}
解决方案 »
- gridview datasource dataview
- 请问一下网站需要展示的图片太多,该怎么处理
- 新建一个空web窗体,发现一个莫名其妙的编译问题,求解决办法.
- 为什么用RegisterStartupScript注册脚本,在网页原文件中搜索不到呢?
- 大家都用哪个公司提供的虚拟主机?北京有没有便宜一点的。
- 新手学ASP.NET,请叫一个问题???
- 我想把dataset1的Tables[0].Rows[0]移到dataset2的Tables[0]下,请问怎么操作呀
- 一个小问题:xml的属性的值有&怎么办?
- Datagrid 如何用Javestrip 获取当前选定行的某个值!!
- 请教各位:
- .net如何判断登陆用户是否下线?
- 在母版页中,发生Microsoft JScript 运行时错误: 'Ext.Net.DirectMethod' 为空或不是对象的错去
//将xmlHttpReq.responseText的值赋给iptText控件
=》
document.getElementById("iptText").value = xmlHttpReq.responseText;
//将xmlHttpReq.responseText的值赋给iptText控件
document.all.iptText.value=xmlHttpReq.responseText;这样就行了
但我请问一下,这两者使用上有什么区别?