首先申明:本文的代碼等,是看了微軟的視頻教程後,照著視頻講義寫的,同時,也用於公司內部培訓網站文章內容
ajax服務端編程
伺服器端代碼
首先定義一個類:Employee public class Employee<br />{<br />public Employee(string fristName,string lastName)<br />{<br />this.mFirstName=firstName;<br />this.mLastName=lastName;<br />}<br />private string mFirstName;<br />public string FirstName<br />{<br />get<br />{<br />return this.mFirstName;<br />}<br />}<br />private string mLastName;<br />public string LastName<br />{<br />get<br />{<br />return this.mLastName;<br />}<br />}<br />}<br />
然後定義一個GetEmployee.ashx,代碼如下: public class GetEmployee:IHttpHandler<br />{<br />public void ProcessRequest(HttpContext context)<br />{<br />context.Response.ContentType="text/plain";<br />string firstName=context.Request.Params("FirstName");<br />string lastName=context.Request.Params("LastName");<br />Employee employee=new Employee(firstName,lastName);<br />//序列化為一個json對象<br />JavaScriptSerializer serializer=new JavaScriptSerializer();<br />string jsonEmployee=serializer.Serialize(employee);<br />context.Response.Write(jsonEmployee);<br />}<br />}<br />
用戶端程式
<br /><input type="button" value="button" onclick="showEmployee('yan','changgang');" /><br />