首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JQuery ajax调用asp.net的webMethod

JQuery ajax调用asp.net的webMethod

作者头像
跟着阿笨一起玩NET
发布2018-09-19 11:43:17
2.2K0
发布2018-09-19 11:43:17
举报

在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>
votes
votes

.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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-01-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档