asp. net USES Ajax and Jquery to pass parameters in the foreground to the background and return instances of values

  • 2020-06-12 08:50:19
  • OfStack

1 "at the front desk

First you need Jquer's package


<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",// The way values are passed 
                    url: "WebAjaxForMe.aspx/GetValueAjax",//WebAjaxForMe.aspx Is the target file, GetValueAjax Is the method in the target file 
                    data: "{username:'" + username + "'}",//username  Is to ask the parameters passed in the background (the parameters here are optional) 
                    success: function (result) {
                        alert(result.d);//result.d Is the parameter returned in the background 
                    }
                })
            })
        })
    </script>
// Here is the source of the parameters 
        <input id="txtUserName" type="text" />

2, the background

In the background first add using System.Web.Services; A reference to the


[WebMethod]// Method must be added before  [WebMethod]       
  public static string GetValueAjax(string username)// The method has to be static and the method has to use keywords static        
{
            // This is where you can do anything with the parameters you pass in             
    return username;     
 }


Related articles: