常规轮询请求,客户端用Ajax调webservice的方法

服务端发布webservice,下图方框中的一定要有

客户端代码

<script type="text/javascript">
        $(document).ready(function () {
            getWebservice();
            setInterval(getWebservice, 1000);
        });
        var swic = true;
        var count = 1;
        function getWebservice() {
            //调用无参数方法
            $.ajax({
                type: "POST",
                contentType: "application/json;charset=utf-8", //WebService 会返回Json类型
                url: "http://localhost:3933/WebService/InterfaceService.asmx/HelloWorld",
                data: "{}", //Email参数
                dataType: 'json',
                success: function (result) {
                    var text = "获取请求"+count+"次:"+result.d.toString()+"";
                    $("#txtContent").text(text)
                    if (swic) {
                        $("#txtContent").attr("style", "color:red");
                        swic = false;
                    }
                    else {
                        $("#txtContent").attr("style", "color:blue");
                        swic = true;
                    }
                    count++;
                },
                error: function (r, s, e) {
                    debugger;
                    //请求失败时的回调处理. 
                    alert(e);
                }
            });
        }
    </script>

  

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        欢迎使用 ASP.NET!
    </h2>
    <p>
        若要了解关于 ASP.NET 的详细信息,请访问 <a href="http://www.asp.net/cn" title="ASP.NET 网站">www.asp.net/cn</a>。
    </p>
    <p>
        您还可以找到 <a href="http://go.microsoft.com/fwlink/?LinkID=152368" title="MSDN ASP.NET 文档">
            MSDN 上有关 ASP.NET 的文档</a>。
    </p>
    <a id="txtContent">获取请求1次</a>
</asp:Content>

前台页面显示

原文地址:https://www.cnblogs.com/liangwenchao-912/p/4915768.html