jQuery调用Asp.net Ajax页面中的方法
2008-11-18 15:48
555 查看
原来用jQuery的ajax方式调用asp.net页面基本都是调用单个页面,由调用页面Response内容,而现在采用asp.net ajax后,我们则可以更完美的使用jQuery和asp.net结合了,代码如下:
1、设置$.ajax中的data,如:data: “{'fname':'Freeze', 'lname':'Soul'}”,注意外围双引号
2、设置$.ajax中的beforeSend,如:
beforeSend: function(xhr) {
xhr.setRequestHeader("Content-type",
"application/json; charset=utf-8");},
3、然后修改GetDate()方法为GetDate(String frname,String lname)
<asp:ScriptManager ID="ScriptManager1" runat="server" > <Scripts> <asp:ScriptReference Path="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" ScriptMode="Release" /> </Scripts> </asp:ScriptManager> <div id="Result">Click here.</div> <script language="javascript"> $(document).ready(() { $("#Result").click(() { $.ajax({ type: "POST", url: "WebForm1.aspx/GetDate", data: {}, contentType: "application/json; charset=utf-8", dataType: "json", success: (msg) { // 替换返回内容 $("#Result").text(msg); }, error: (xhr,msg,e) { alert(msg);} }); }); }); </script>WebForm1.aspx(页面GetDate方法必须使用静态方法并且使用[System.Web.Services.WebMethod]属性,如调用[b]webservice[/b]中的方法不需要静态)
public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [System.Web.Services.WebMethod] public static string GetDate() { return DateTime.Now.ToString(); } }注:带参数时
1、设置$.ajax中的data,如:data: “{'fname':'Freeze', 'lname':'Soul'}”,注意外围双引号
2、设置$.ajax中的beforeSend,如:
beforeSend: function(xhr) {
xhr.setRequestHeader("Content-type",
"application/json; charset=utf-8");},
3、然后修改GetDate()方法为GetDate(String frname,String lname)
相关文章推荐
- jQuery调用Asp.net Ajax页面中的方法
- jQuery调用Asp.net Ajax页面中的方法
- jquery-ajax之2:调用asp.net 页面后台方法
- jQuery调用Asp.net Ajax页面中的方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- asp.net jquery.Ajax() 方法调用后台方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- jquery调用asp.net 页面后台方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(0):目录
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(5):服务器端和客户端数据类型的自动转换:基本类型和枚举类型
- jquery调用asp.net 页面后台方法
- jquery调用asp.net 页面后台方法
- 利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法
- asp.net ajax1.0基础回顾(六):调用ASPX页面方法 推荐
- ASP.net AJAX 调用PageMethods实例(javascript调用服务器页面方法)
- 利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(8):服务器端和客户端数据类型的自动转换:数组类型
- jQuery.ajax()调用asp.net后台方法
- jquery调用asp.net 页面后台方法