JQuery AJAX calls the page background Method

Source: Internet
Author: User

1. Create a demo. aspx page.
2. Add references to the background file demos. aspx. cs on the page.

using System.Web.Services;
3. Call a method without parameters.
Note that this version cannot be lower than. net framework 2.0. 2.0 is not supported.
Background code:
[WebMethod]     
public static string SayHello()
{
return "Hello Ajax!";
}
JS Code:
$ (Function (){
$ ("# BtnOK"). click (function (){
$. Ajax ({
// Use the post Method
Type: "Post ",
// Method page and method name
Url: "Demo. 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;
});
});
Page code:
<Form id = "form1" runat = "server">
<Div>
<Asp: Button ID = "btnOK" runat = "server" Text = "verify user"/>
</Div>
</Form>
The running effect is as follows:

3. Call methods with Parameters
Background code:
[WebMethod]     
public static string GetStr(string str, string str2)
{
return str + str2;
}
JS Code:
$ (Function (){
$ ("# BtnOK"). click (function (){
$. Ajax ({
Type: "Post ",
Url: "demo. 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;
});
});
The running effect is as follows:

4. Return Array Method
Background code:

[WebMethod]     
public static List<string> GetArray()
{
List<string> li = new List<string>();

for (int i = 0; i < 10; i++)
li.Add(i + "");

return li;
}

JS Code:

$ (Function (){
$ ("# BtnOK"). click (function (){
$. Ajax ({
Type: "Post ",
Url: "demo. 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;
});
});

Page code:

<form id="form1" runat="server"><div>    <asp:Button ID="btnOK" runat="server" Text="Verify the user" /></div><ul id="list"></ul></form>

Running result diagram:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.