Ajax非同步呼叫ashx返回序列化類別

來源:互聯網
上載者:User

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 = new Employee(firstName, lastName, title);                        JavaScriptSerializer serializer = new JavaScriptSerializer();            string jsonEmp = serializer.Serialize(employee);                        context.Response.Write(jsonEmp);        }        public bool IsReusable        {            get            {                return false;            }        }    }}

Employee.cs類代碼

View Code

using System;using System.Data;using System.Configuration;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;namespace AspNetAjaxOverview{    /// <summary>    /// Summary description for Employee    /// </summary>    public class Employee    {        private string _FirstName;        private string _LastName;        private string _Title;        public Employee() { }        public Employee(string firstName, string lastName, string title)        {            this._FirstName = firstName;            this._LastName = lastName;            this._Title = title;        }        public string FirstName        {            get            {                return this._FirstName;            }        }        public string LastName        {            get            {                return this._LastName;            }        }        public string Title        {            get            {                return this._Title;            }        }    }}

異表調用ashx代碼

<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>Asynchronous Communication Layer Overview</title></head><body>    <form id="form1" runat="server">        <asp:ScriptManager ID="ScriptManager1" runat="server">        </asp:ScriptManager>                <script language="javascript" type="text/javascript">            function showEmployee(firstName, lastName, title)            {                var request = new Sys.Net.WebRequest();                request.set_url('GetEmployee.ashx');                request.set_httpVerb("POST");                request.add_completed(onGetEmployeeComplete);                                var requestBody = String.format(                    "firstName={0}&lastName={1}&title={2}",                    encodeURIComponent(firstName),                    encodeURIComponent(lastName),                    encodeURIComponent(title));                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 type="button" value="Bill Gates"            onclick="showEmployee('Bill', 'Gates', 'Chair man')" />        <input type="button" value="Steve Ballmer"            onclick="showEmployee('Steve', 'Ballmer', 'CEO')" />    </form></body></html>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.