asp.net利用Ajax和Jquery在前台向后台傳參數並返回值


1》前台

首先需要 Jquer的包

<script src="js/jquery-1.9.1.js" type="text/javascript"></script>

下面是     <script type="text/javascript">

        $(function () {

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

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

                $.ajax({

                    type: "post",

                    contentType: "application/json",//傳值的方式

                    url: "WebAjaxForMe.aspx/GetValueAjax",//WebAjaxForMe.aspx為目標文件,GetValueAjax為目標文件中的方法

                    data: "{username:'" + username + "'}",//username 為想問后台傳的參數(這里的參數可有可無)

                    success: function (result) {

                        alert(result.d);//result.d為后台返回的參數

                    }

                })

            })

        })

    </script>

這里是參數的來源

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

2》后台

在后台首先要添加using System.Web.Services;的引用

[WebMethod]//方法前邊必須添加 [WebMethod]       

  public static string GetValueAjax(string username)//這個方法需要是靜態的方法要用到關鍵字static        

{

            //在這里可以對傳進來的參數進行任何操作            

    return username;     

 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM