Example of asp.net that uses Ajax and Jquery to transmit parameters to the background and return values in the foreground

Source: Internet
Author: User

1. Front-end

First, we need the Jquer package.
Copy codeThe Code is as follows:
<Script src = "js/jquery-1.9.1.js" type = "text/javascript"> </script>

The following is <script type = "text/javascript">

$ (Function (){

$ ('# TxtUserName'). blur (function (){

Var username = $ (this). val ();

$. Ajax ({

Type: "post ",

ContentType: "application/json", // method of passing values

Url: "WebAjaxForMe. aspx/GetValueAjax", // WebAjaxForMe. aspx is the target file, and GetValueAjax is the method in the target file

Data: "{username: '" + username + "'}", // username indicates the parameter to be passed in to the background (the parameter here is optional)

Success: function (result ){

Alert (result. d); // result. d is the parameter returned by the background.

}

})

})

})

</Script>

// Here is the source of the Parameter

<Input id = "txtUserName" type = "text"/>

2. Background

Add using System. Web. Services;

Copy codeThe Code is as follows:
[WebMethod] // you must add [WebMethod] to the front of the method.

Public static string GetValueAjax (string username) // This method must be a static method. The keyword static is used.

{

// You can perform any operation on the passed parameters.

Return username;

}

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.