[WebMethod] namespace
1. method calls without parameters. Note: 1. The method must be a static method and must have a [WebMethod] Declaration.
Background <C #>:
Using System. Web. Script. Services;
[WebMethod]
Public static string SayHello ()
{
Return "Hello Ajax! ";
}
Foreground <JQuery>:
$ (Function (){
$ ("# BtnOK"). click (function (){
$. Ajax ({
// Use the post Method
Type: "Post ",
// Method page and method name
Url: "data. aspx/SayHello ",
ContentType: "application/json; charset = UTF-8 ",
DataType: "json ",
Success: function (data ){
// Use data. d to obtain the returned data.
Alert (data. d );
},
Error: function (err ){
Alert (err );
}
});
// Disable button submission
Return false;
});
});
2. Call methods with Parameters
Background <C #>:
Using System. Web. Script. Services;
[WebMethod]
Public static string GetStr (string str, string str2)
{
Return str + str2;
}
Foreground <JQuery>:
$ (Function (){
$ ("# BtnOK"). click (function (){
$. Ajax ({
Type: "Post ",
Url: "data. aspx/GetStr ",
// Method parameters must be written correctly. str is the name of the parameter, and str2 is the name of the second parameter.
Data: "{'str': 'I am', 'str2': 'xxx '}",
ContentType: "application/json; charset = UTF-8 ",
DataType: "json ",
Success: function (data ){
// Use data. d to obtain the returned data.
Alert (data. d );
},
Error: function (err ){
Alert (err );
}
});
// Disable button submission
Return false;
});
});
3. Call the Array Method
Background <C #>:
Using System. Web. Script. Services;
[WebMethod]
Public static List <string> GetArray ()
{
List <string> li = new List <string> ();
For (int I = 0; I <10; I ++)
Li. Add (I + "");
Return li;
}
Foreground <JQuery>:
$ (Function (){
$ ("# BtnOK"). click (function (){
$. Ajax ({
Type: "Post ",
Url: "data. aspx/GetArray ",
ContentType: "application/json; charset = UTF-8 ",
DataType: "json ",
Success: function (data ){
// Clear ul before insertion
$ ("# List" ).html ("");
// Recursively retrieve data
$ (Data. d). each (function (){
// Insert the result to li
$ ("# List"). append ("<li>" + this + "</li> ");
});
Alert (data. d );
},
Error: function (err ){
Alert (err );
}
});
// Disable button submission
Return false;
});
});
// <Reference path = "jquery-1.4.2-vsdoc.js"/>
$ (Function (){
$ ("# BtnOK"). click (function (){
$. Ajax ({
Type: "Post ",
Url: "data. aspx/GetArray ",
ContentType: "application/json; charset = UTF-8 ",
DataType: "json ",
Success: function (data ){
// Clear ul before insertion
$ ("# List" ).html ("");
// Recursively retrieve data www.2cto.com
$ (Data. d). each (function (){
// Insert the result to li
$ ("# List"). append ("<li>" + this + "</li> ");
});
Alert (data. d );
},
Error: function (err ){
Alert (err );
}
});
// Disable button submission
Return false;
});
});
4. Return the call of the Hashtable method.
Background <C #>:
Using System. Web. Script. Services;
Using System. Collections;
[WebMethod]
Public static Hashtable GetHash (string key, string value)
{
Hashtable hs = new Hashtable ();
Hs. Add ("www", "yahooooooo ");
Hs. Add (key, value );
Return hs;
}
Foreground <JQuery>:
$ (Function (){
$ ("# BtnOK"). click (function (){
$. Ajax ({
Type: "Post ",
Url: "data. aspx/GetHash ",
// Remember to add double quotation marks T_T
Data: "{'key': 'hahaha', 'value': 'haha! '}",
ContentType: "application/json; charset = UTF-8 ",
DataType: "json ",
Success: function (data ){
Alert ("key: haha =>" + data. d ["haha"] + "\ n key: www =>" + data. d ["www"]);
},
Error: function (err ){
Alert (err + "err ");
}
});
// Disable button submission
Return false;
});
});
5. Operate xml
XMLtest. xml:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Data>
<Item>
<Id> 1 </id>
<Name> qwe </name>
</Item>
<Item>
<Id> 2 </id>
<Name> asd </name>
</Item>
</Data>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Data>
<Item>
<Id> 1 </id>
<Name> qwe </name>
</Item>
<Item>
<Id> 2 </id>
<Name> asd </name>
</Item>
</Data>
Foreground <JQuery>:
$ (Function (){
$ ("# BtnOK"). click (function (){
$. Ajax ({
Url: "XMLtest. xml ",
DataType: 'xml', // The returned type is xml, which is different from the preceding Json.
Success: function (xml ){
// Clear the list
$ ("# List" ).html ("");
// Search for xml elements
$ (Xml). find ("data> item"). each (function (){
$ ("# List "). append ("<li> id:" + $ (this ). find ("id "). text () + "</li> ");
$ ("# List "). append ("<li> Name:" + $ (this ). find ("name "). text () + "</li> ");
})
},
Error: function (result, status) {// if no above capture error occurs, the callback function here will be executed
Alert (status );
}
});
// Disable button submission
Return false;
});
});
From chinacache-henw