Entity:
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Runtime. Serialization;
Namespace Entity
{
[DataContract]
Public class User
{
[DataMember]
Public string Name
{
Get;
Set;
}
[DataMember]
Public int Age
{
Get;
Set;
}
}
}
WebService:
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
Using Entity;
Namespace JQuery. Handler
{
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[System. ComponentModel. ToolboxItem (false)]
[System. Web. Script. Services. ScriptService]
Public class UserService1: System. Web. Services. WebService
{
[WebMethod]
Public string ComplexType (User hero, List <User> users)
{
Return hero. Name + "has" + users. Count + "people! ";
}
}
}
Html:
Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Ajax </title>
<Script src = "../Scripts/jquery-1.6.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
$ ("# BtnWeb"). click (function (){
$. Ajax (
{
Type: "post ",
Url: "../Handler/UserService. asmx/ComplexType ",
DataType: "json ",
ContentType: "application/json ",
Data: '{"hero": {"Name": "zhoulq", "Age": 27}, "users": [{"Name": "zhangs ", "Age": 22 },{ "Name": "wangw", "Age": 26 },{ "Name": "liuj", "Age": 25 },
{"Name": "luos", "Age": 24}]} ',
Success: function (data) {$ ("# web"). text (data. d );}
});
});
});
</Script>
</Head>
<Body>
<Input id = "btnWeb" type = "button" value = "request WebService"/> <label id = "web"> </label>
</Body>
</Html>