BookInfo.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ClassProj { [Serializable] publicclass BookInfo { public BookInfo() { } //在VS2008中可使用如下简化方式定义属性,是不是很简单了呢?! publicstring Name { set; get; } publicstring Author { set; get; } publicint Price { set; get; } } }
Book.asmx.cs using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using System.Web.Script.Services; using ClassProj; namespace Test { /**////<summary> /// Book 的摘要说明 ///</summary> [WebService(Namespace ="http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [ScriptService] [GenerateScriptType(typeof(BookInfo))] publicclass Book : System.Web.Services.WebService { [WebMethod] publicstring ShowInfo(BookInfo book) { return"书的信息如下——书名:"+ book.Name +",价格:"+ book.Price +"作者:"+ book.Author; } } }
Test.js function ShowInfo() { var book=new ClassProj.BookInfo(); book.Name="征服AJAX!"; book.Price=25; book.Author="梦想"; Test.Book.ShowInfo(book,CallBack_Function); } function CallBack_Function(ResponseStr) { $get("result").innerText=ResponseStr; }