1. 最原始的ajax请求方式
(1). get请求
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxDemo.aspx.cs" Inherits="ajax_AjaxDemo" %> 2 3<!DOCTYPE html> 4 5<html xmlns="http://www.w3.org/1999/xhtml"> 6<head runat="server"> 7<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8 <title></title> 9 <script src="../js/jquery-1.7.1.js"></script> 10 <script type="text/javascript"> 11 $(function () { 12 $("#btnGetDate").click(function () { 13 //开始通过AJAX向服务器发送请求. 14 var xhr; 15 if (XMLHttpRequest) {//表示用户使用的高版本IE,谷歌,狐火等浏览器 16 xhr = new XMLHttpRequest(); 17 } else {// 低IE 18 xhr = new ActiveXObject("Microsoft.XMLHTTP"); 19 } 20 xhr.open("get", "GetDate.ashx?name=zhangsan&age=12", true); 21 xhr.send();//开始发送 22 //回调函数:当服务器将数据返回给浏览器后,自动调用该方法。 23 xhr.onreadystatechange = function () { 24 if (xhr.readyState == 4) {//表示服务端已经将数据完整返回,并且浏览器全部接受完毕。 25 if (xhr.status == 200) {//判断响应状态码是否为200. 26 27 alert(xhr.responseText); 28 29 } 30 } 31 } 32 }); 33 }); 34 </script> 35</head> 36<body> 37 <form id="form1" runat="server"> 38 <div> 39 <input type="button" value="获取服务端时间" id="btnGetDate" /> 40 </div> 41 </form> 42</body> 43</html>
(2). post请求
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxPostDemo.aspx.cs" Inherits="ajax_AjaxPostDemo" %> 2 3<!DOCTYPE html> 4 5<html xmlns="http://www.w3.org/1999/xhtml"> 6<head runat="server"> 7<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8 <title></title> 9 <script src="../js/jquery-1.7.1.js"></script> 10 <script type="text/javascript"> 11 $(function () { 12 $("#btnPost").click(function () { 13 var xhr; 14 if (XMLHttpRequest) { 15 xhr = new XMLHttpRequest(); 16 } else { 17 xhr = new ActiveXObject("Microsoft.XMLHTTP"); 18 } 19 xhr.open("post", "GetDate.ashx", true); 20 // 表示想服务端发送的请求都放在请求报文体中,并且以下面的形式发送出去 21 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 22 xhr.send("name=zhangsan&pwd=123"); 23 xhr.onreadystatechange = function () { 24 if (xhr.readyState == 4) { 25 if (xhr.status == 200) { 26 alert(xhr.responseText); 27 } 28 } 29 } 30 }); 31 }); 32 </script> 33</head> 34<body> 35 <form id="form1" runat="server" > 36 <div> 37 <input type="button" value="获取数据" id="btnPost" /> 38 39 </div> 40 </form> 41</body> 42</html>
(3). 请求的公共ashx文件

1<%@ WebHandler Language="C#" Class="GetDate" %> 2 3using System; 4using System.Web; 5 public class GetDate : IHttpHandler 6 { 7 8 public void ProcessRequest(HttpContext context) 9 { 10 context.Response.ContentType = "text/plain"; 11 // context.requrest该方法会自动判断请求的方式是get还是post 12 context.Response.Write(context.Request["name"]); 13 } 14 15 public bool IsReusable 16 { 17 get 18 { 19 return false; 20 } 21 } 22 }
View Code
(4) .通过Jquery请求Ajax的方式

1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JqueryAjax.aspx.cs" Inherits="ajax_JqueryAjax" %> 2 3<!DOCTYPE html> 4 5<html xmlns="http://www.w3.org/1999/xhtml"> 6<head runat="server"> 7<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8 <title></title> 9 <script src="../js/jquery-1.7.1.js"></script> 10 <script type="text/javascript"> 11 $(function () { 12 $("#btnGet").click(function () { //回调函数 13 $.get("GetDate.ashx", { "name": "lisi", "pwd": "123" }, function (data) { 14 alert(data) 15 }); 16 17 }); 18 19 $("#btnPost").click(function () { 20 $.post("GetDate.ashx", { "name": "lisi", "pwd": "123" }, function (data) { 21 alert(data) 22 }) 23 }); 24 25 26 $("#btnAjax").click(function () { 27 $.ajax({ 28 type: "POST", //请求类型 29 url: "GetDate.ashx", //请求地址 30 data: "name=John&location=Boston", //请求参数 31 success: function (msg) { //回调函数 32 alert("Data Saved: " + msg); 33 } 34 }); 35 36 }); 37 38 39 }); 40 </script> 41</head> 42<body> 43 <form id="form1" runat="server"> 44 <div> 45 <input type="button" value="GET获取数据" id="btnGet" /> 46 <input type="button" value="POST获取数据" id="btnPost" /> 47 <input type="button" value="AJAX获取数据" id="btnAjax" /> 48 </div> 49 </form> 50</body> 51</html>
View Code
(5). serializeArray方法的使用
1//将对象转成json对象传递给后端 2$("button").click(function(){ 3 var par =$("form表单id的值").serializeArray(); 4 });