JqueryAjax异步加载在ASP.NET

前台代码

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

<script type="text/javascript">

    function ShowCurrentTime() {

        $.ajax({

            url: "webService.aspx/GetCurrentTime",//请求的页面/页面处理数据的方法

            type: "POST",                            //传输方式

            contentType: "application/json;charset=utf-8", //编码

            dataType: "json",                             //格式

            data: '{name: "' + $("input[name='name']").val() + '" ,age:"' + $("input[name='age']").val() + '"}',//json字符串

            success: function (data) {                        //成功以后的回调函数

                alert(data.d);

            },

            failure: function (data) {

                alert(data.d);

            }

        })

    }

</script>

<body>

    <form id="form1" runat="server">

    <div>

    <input type="text" name="name" />

    <input type="text" name="age" />

    <input type="button" value="获取当前时间" onclick="ShowCurrentTime()" />

    </div>

    </form>

webService.aspx后台代码:

[System.Web.Services.WebMethod]                      //将 WebMethod 属性附加到 Public 方法表示希望将该方法公开为 XML Web services 的一部分

        public static string GetCurrentTime(string name,string age)

        {

            return "Hello"+name+age+DateTime.Now.ToString();

        }

 

你可能感兴趣的:(asp.net)