Jquery calls the Asp.net page background Method

Source: Internet
Author: User

Create An ASPX page and write a client Control <input type = "button" id = "ajaxdemo" value = "ajaxdemo">

Then, write a simple method on the aspx background page,CodeAs follows:

[Webmethod]
Public static string ABC (string ABC)
{
Return ABC;
}

Must be declared as static methods, and they must be marked using the [webmethod] feature.

 

Next, we should consider how to make the front-end client control call the method to the back-end. At this time, jqury was launched ..

 

Introduce the jquery class library on the page

<SCRIPT type = "text/JavaScript" src = "jquery/jquery-1.3.2-vsdoc2.js"> </SCRIPT>

Add the script code on the page as follows:

<SCRIPT type = "text/JavaScript">

$ (). Ready (
Function (){
$ ("# Ajaxdemo"). Click (function (){
$. Ajax ({
Type: "Post ",
URL: "default. aspx/ABC ",
Data: "{'abc': 'test '}",
Contenttype: "application/JSON; charset = UTF-8 ",
Success: function (MSG) {alert (MSG );}
})
})
}
)

</SCRIPT>

 

This is all done! Many things have been completed by the jquery class library. Here we will discuss how to use it and how to implement it. We don't care about it!

There are still many methods like $. Ajax in jquery for us to use. You can try it!

 

PS: I am just a beginner. Here I am just taking notes! If something is wrong, please point it out!

 

Note: You need to build a 3.5 project, if it is 2.0. There will be a lot of references in the configuration file, if you build a 2.0 project. Create 3.5. Overwrite the configuration file of 3.5 to the project of 2.0!

If "{d: Test}" is displayed after the above Code is successful, because it returns a string, we can change it to return a JSON object.

Modify the jquery Code as follows:

$ (). Ready (
Function (){
$ ("# Ajaxdemo"). Click (function (){
$. Ajax ({
Type: "Post ",
URL: "default. aspx/ABC ",
Data: "{'abc': 'test '}",
Datatype: "JSON ",
Contenttype: "application/JSON; charset = UTF-8 ",
Success: function (MSG) {alert (msg. d );}
})
})
}
)

 

We set the returned data to be a JSON object. Now we can use the returned JSON object. According to the pop-up D: test, we can see that the key is d and the value is test, then we can use the returned data MSG object to directly point D to get the test, after modifying the code. now it is test ..

 

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.