前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JQuery ajax调用asp.net的webMethod

JQuery ajax调用asp.net的webMethod

作者头像
跟着阿笨一起玩NET
发布2018-09-19 11:42:29
2K0
发布2018-09-19 11:42:29
举报

本文章转载:https://cloud.tencent.com/developer/article/1342717

在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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 甩掉 ashx/asmx,使用jQuery.ajaxWebService请求WebMethod,Ajax处理更加简练 
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档