AjaxTest.aspx.cs: protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["s"] == "1")//使用查询字串来指示这个请求是通过Ajax发出的 { Response.Write("hello world!");//向HttpResponse中输出hello world! Response.End();//将页面缓冲发送向客户端浏览器 并中止该页输出 //如果去掉这句 会得到多余的HTML代码 } }相对来说,我们在前台页面中书写的代码将会多一些,慢慢地你会发现这也许是Ajax的一个惯例:AjaxTest.aspx:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxTest.aspx.cs" Inherits= "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 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.getElementById("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>
解决方案 »
- ckeditor 自己添加了事件,然后 setData("值") 操作后事件失效,再次绑定也不成功???谁有好的解决方法,急!急!急!急!急!急!急!急!
- 在网页中显示日期
- 关于获取控件宽度的问题
- js读取服务器端文件内容,形成网页,该怎么做?
- 在线等,急急急啊!那位好汉进来帮忙关于ASP和JAVASCRIPT的问题
- 菜鸟请教 单击一个按钮执行js文件中的函数,为什么没反应
- 这叫什么语法? <ilayer id="main" width=&{scrollerwidth}; height=&{scrollerheight}; bgcolor=&{scrollerbgcolor};>
- 聊天室程序的一些小问题
- 如何在网页中写语句让IE的工具栏和地址不显示并定义IE窗口大小?
- JavaScript小白求助
- 关于javascript导航高亮的代码解释
- json提交问题
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testajax.aspx.cs" Inherits="myJQ_testajax" %><!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 runat="server">
<title>json</title> <script type="text/javascript" src="jquery-1.4.js" charset="utf-8"></script> <script type="text/javascript">
$(function(){
$("#btok").click(function(){
var strname = encodeURI($("#name").val());
var straddress = encodeURI($("#address").val());
$.ajax({
url:"json.ashx?name="+strname+"&address="+straddress,
dataType:"json",
success:function(json){
alert(decodeURI(json.name));
alert(decodeURI(json.address));
},
error:function(r){
alert(r);
}
});
});
});
</script></head>
<body>
<form id="form1" runat="server">
name:<input type="text" value="liujia" id="name" /><br>
address:<input type="text" value="hncz" id="address" /><br>
<input type="button" value="ajaxJson" id="btok" /><br>
</form>
</body>
</html><%@ WebHandler Language="C#" Class="json" %>using System;
using System.Web;public class json : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string str = context.Request.QueryString["name"];
string str2 = context.Request.QueryString["address"];
context.Response.Write("{\"name\":\"" + str + "\",\"address\":\"" + str2 + "\"}");
}
public bool IsReusable {
get {
return false;
}
}
}