[WebMethod] namespace 1. Call a method without parameters. Note: 1. the method must be static, and the declaration background of [WebMethod] should be included & lt; C # & gt;: usingSystem. web. script. services; [WebMethod] publicstaticstringSayHello () {return & quot ;...
[WebMethod] namespace
1. method calls without parameters. Note: 1. The method must be a static method and must have a [WebMethod] Declaration.
Background :
Using System. Web. Script. Services;
[WebMethod]
Public static string SayHello ()
{
Return "Hello Ajax! ";
}
Front-end :
$ (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 :
Using System. Web. Script. Services;
[WebMethod]
Public static string GetStr (string str, string str2)
{
Return str + str2;
}
Front-end :
$ (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 :
Using System. Web. Script. Services;
[WebMethod]
Public static List GetArray ()
{
List Li = new List ();
For (int I = 0; I <10; I ++)
Li. Add (I + "");
Return li;
}
Front-end :
$ (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 ("
"+ This +"");
});
Alert (data. d );
},
Error: function (err ){
Alert (err );
}
});
// Disable button submission
Return false;
});
});
///
$ (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 ("
"+ This +"");
});
Alert (data. d );
},
Error: function (err ){
Alert (err );
}
});
// Disable button submission
Return false;
});
});
4. Return the call of the Hashtable method.
Background :
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;
}
Front-end :
$ (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:
1
Qwe
2
Asd
1
Qwe
2
Asd
Front-end :
$ (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 ("
Id: "+ $ (this). find (" id "). text () +"");
$ ("# List"). append ("
Name: "+ $ (this). find (" name "). text () +"");
})
},
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