在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({ "0%" }).animate({ percentage }, 'slow'); }); } </script>
.net2.0 Web.config:
<!--JQuery ajax调用asp.net的webMethod问题 2.0--> <httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </httpModules>