這篇部落格我們來看ASP.NET AJAX中資料的交換過程。
Employee類:
/// <summary>/// Employee類/// </summary>public class Employee{private string _FirstName;private string _LastName;private string _Title;public Employee() { } /// <summary> /// 帶參數建構函式 /// </summary> /// <param name="firstName">姓</param> /// <param name="lastName">名</param> /// <param name="title">職位</param>public Employee(string firstName, string lastName, string title){this._FirstName = firstName;this._LastName = lastName;this._Title = title;} /// <summary> /// 姓 /// </summary>public string FirstName{get{return this._FirstName;}} /// <summary> /// 名 /// </summary>public string LastName{get{return this._LastName;}} /// <summary> /// 職位 /// </summary>public string Title{get{return this._Title;}}}
用戶端代碼
GetEmployee.ashx
<%@ WebHandler Language="C#" Class="AspNetAjaxOverview.GetEmployee" %>using System;using System.Web;using System.Web.Script.Serialization;namespace AspNetAjaxOverview{public class GetEmployee : IHttpHandler{public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/plain";//擷取姓、名、職位string firstName = context.Request.Params["firstName"];string lastName = context.Request.Params["lastName"];string title = context.Request.Params["title"]; //執行個體Employee類Employee employee = new Employee(firstName, lastName, title); //序列化為json字串JavaScriptSerializer serializer = new JavaScriptSerializer();string jsonEmp = serializer.Serialize(employee);//響應請求context.Response.Write(jsonEmp);}public bool IsReusable{get{return false;}}}}
服務端代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="2非同步通訊.aspx.cs" Inherits="_2非同步通訊" %><!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></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <script type="text/javascript" > function showEmployee(firstName, lastName, title) { //將字串轉義 var requestBody = String.format("firstName={0}&lastName={1}&title={2}",encodeURIComponent(firstName),encodeURIComponent(lastName),encodeURIComponent(title)); var request = new Sys.Net.WebRequest(); //請求服務端地址 request.set_url('GetEmployee.ashx'); //請求方式:POST request.set_httpVerb("POST"); //註冊回呼函數 request.add_completed(onGetEmployeeComplete); //佈建要求 request.set_body(requestBody); //提交請求request.invoke();} //回呼函數function onGetEmployeeComplete(response) { //響應是否可用if (response.get_responseAvailable()) { //擷取對象 var employee = response.get_object(); //直接使用對象的屬性即可alert(String.format("Hello I'm {0} {1}, my title is '{2}'",employee.FirstName,employee.LastName,employee.Title));}}</script><!--提示姓名、職位--><input id="Button1" type="button" value="Bill Gates"onclick="showEmployee('Bill', 'Gates', 'Chair man')" /><input id="Button2" type="button" value="Steve Ballmer"onclick="showEmployee('Steve', 'Ballmer', 'CEO')" /> </form></body></html>
運行結果
資料交換過程,為了直觀展示,採用時序圖方式:
可以看到中間的JavaScript就相當於一個非同步通訊層,負責對browser和server之間進行資料交換。