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;
}