本文章转载:http://www.cnblogs.com/zengxiangzhan/archive/2011/01/16/1936938.html
在vs2010中,用JQuery ajax调用asp.net 2.0的 webMethod 方法时,怎么都调不出来,原来和3.5 有点出入。
3.5中,无需特殊设置,可以直接用$.ajax调用在aspx.cs中,访问级别public,静态的,标记为【webmethod】的方法。
aspx.cs:
using System.Web.Services;
[WebMethod]
public static string PollCount()
{
…… return getResultHTML(ds2);
}
jquery:
<div id="divAnswers" runat="server">
</div>
<script src="scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
var imgPoll = new Image();
imgPoll.src = 'style/red-bar.png';
$.ajax(
{
type: "POST",
contentType: "application/json",
url: "voteresult.aspx/PollCount",
data: "{}",
dataType: "json",
success: function (msg)
{
$("div[id$=divAnswers]").fadeOut("fast").html(msg.d).fadeIn("fast", function () { animateResults(); });
}
});
});
function animateResults() {
$("div[id$=divAnswers] img").each(function () {
var percentage = $(this).attr("val");
$(this).css({ width: "0%" }).animate({ width: percentage }, 'slow');
});
}
</script>
不错的类似文章:
http://www.cnblogs.com/fanyong/p/3463701.html
http://blog.csdn.net/gulijiang2008/article/details/6234940
甩掉 ashx/asmx,使用jQuery.ajaxWebService请求WebMethod,Ajax处理更加简练
在WebForm下 开发ajax程序,需要借助于一般处理程序(*.ashx)或web服务(*.asmx),并且每一个ajax请求,都要建一个这样的文件,如此一来,如果在一个项目中ajax程序多了,势必会产生一堆的.ashx或.asmx,虽然于程序本身无碍,但那一堆文件看上去总觉得有伤大雅。那么可不可以丢掉这些.ashx和.asmx,选择一种更简练的方式来做ajax程序呢。
答案是肯定的,那就是:WebMethod 。
文章地址:http://www.mysjtu.com/page/M0/S231/231122.html