jQuery调用Asp.net Ajax页面中的方法
来源:互联网 发布:东华软件 现金流 编辑:程序博客网 时间:2024/04/28 20:34
原来用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" >WebForm1.aspx(页面GetDate方法必须使用静态方法并且使用[System.Web.Services.WebMethod]属性,如调用webservice中的方法不需要静态)
<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(function() {
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "WebForm1.aspx/GetDate",//注意调用方式,同样方式可以调用webservice
data: {}, //在这里可以设置需要传递的参数
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// 替换返回内容
$("#Result").text(msg);
},
error: function(xhr,msg,e) { alert(msg);}
});
});
});
</script>
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调用Asp.net Ajax页面中的方法
- Jquery Ajax调用ASP.NET 后台页面方法
- jQuery AJax调用asp.net webservers 方法
- jquery ajax调用asp.net 后台方法
- JQuery Ajax调用asp.net后台方法
- jquery调用asp.net的Ajax方法
- jQuery.ajax()调用asp.net后台方法
- jQuery AJax调用asp.net webservers 方法
- JQuery Ajax调用asp.net后台方法
- JQuery Ajax调用asp.net后台方法
- JQuery Ajax调用asp.net后台方法
- jQuery.ajax()调用asp.net后台方法
- jQuery.ajax()调用asp.net后台方法
- jQuery.ajax()调用asp.net后台方法
- jquery调用asp.net 页面后台方法
- jquery调用asp.net 页面后台方法
- sp_executesql 与 execute 的差异
- java中的参数传递
- dotnetcharting 属性
- Heritrix-1.12.1在Eclipse中的配置
- 对数据仓库维度表的理解
- jQuery调用Asp.net Ajax页面中的方法
- vi文本处理器
- [注意]Windows 的版本支持AS最大内存
- 安装memcached
- 我长大了就不找妈妈了……
- jquery $.getJSON服务器端代码 asp.net
- [讨论]部署 Analysis Services 项目时需考虑问题
- 使用Heritrix-1.12.1新建抓取任务
- 什么是SSIS的Checkpoint