• JS中调用Webservice


    1.新建一个WebApplication项目,取默认设置。

    2.添加一个WebService,代码如下:

     1 using System.ComponentModel;
     2 using System.Web.Services;
     3  
     4 namespace WebApplication1
     5 {
     6     /// <summary>
     7     /// WebService1 的摘要说明
     8     /// </summary>
     9     [WebService(Namespace = "http://tempuri.org/")]
    10     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    11     [ToolboxItem(false)]
    12     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    13     [System.Web.Script.Services.ScriptService]
    14     public class WebService1 : System.Web.Services.WebService
    15     {
    16  
    17         [WebMethod]
    18         public string HelloWorldFun1()
    19         {
    20             return "Hello World";
    21         }
    22         [WebMethod]
    23         public string HelloWorldFun2(string str)
    24         {
    25             return "Hello World,"+str;
    26         }
    27     }
    28 }

    3.准备好WebService后,编辑Default.aspx文件。代码如下:

     1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
     2  
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4  
     5 <html xmlns="http://www.w3.org/1999/xhtml" >
     6 <head runat="server">
     7     <title>JS调用WebService</title>
     8     <script type="text/javascript" language="javascript">
     9         function func1()
    10         {        
    11         WebApplication1.WebService1.HelloWorldFun1(onSuccess,onFail,'Span1');
    12         }
    13         function func2()
    14         {
    15         var txt=document.getElementById('Text1').value;
    16         WebApplication1.WebService1.HelloWorldFun2(txt,onSuccess,onFail,'Span2');
    17         }
    18          
    19         function onSuccess(value,context)
    20         {
    21         document.getElementById(context).innerHTML=value;
    22         }
    23         function onFail(value)
    24         {
    25         alert(value);
    26         }
    27     </script>
    28 </head>
    29 <body>
    30     <form id="form1" runat="server">
    31     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    32     <Services>
    33         <asp:ServiceReference Path="~/WebService1.asmx"  />
    34     </Services>
    35     </asp:ScriptManager>
    36     <input id="Button1" type="button" value="button"  onclick="func1()" />    <span id="Span1"></span>
    37     <hr />
    38     <input id="Text1" type="text" /><input id="Button2" type="button" value="button"  onclick="func2()" />    <span id="Span2"></span>
    39     </form>
    40 </body>
    41 </html>

    操作说明:

    1.页面中需要添加ScriptManager组件,然后在里面添加WebService引用声明。[这里的Pah可以换成网络上的WebService路径。]

    2.将ScriptManager的EnablePageMethods属性设置为True。[这是必须的,否则JS不知道该WebService。]

    3.调用的格式:namespace.class.method([param],[onsuccessJSHandle],[onfailHSHandle],context);

       其中:A。context为上下文关联参数,这里设置后,在调用成功的处理函数处可以调用。

          B。onsuccessJSHandle为调用成功后的处理函数。

          C。onfailHSHandle为调用失败后的处理函数。 

    4.WebService编写时需注意:

          AWebService类前必须加 [System.Web.Script.Services.ScriptService]

              BWebService方法前必须加 [WebMethod]

  • 相关阅读:
    BZOJ4669 抢夺(网络流)
    三元环&四元环计数
    P3768 简单的数学题(杜教筛)
    P1829 [国家集训队]Crash的数字表格(莫比乌斯反演)
    P5221 Product(欧拉函数)
    P3704 [SDOI2017]数字表格(莫比乌斯反演)
    P4619 [SDOI2018]旧试题
    Loj6102. 「2017 山东二轮集训 Day1」第三题(min-max 反演)
    Leetcode220 存在重复元素III
    Leetcode219 存在重复元素II 滑动窗口
  • 原文地址:https://www.cnblogs.com/SoraAoi/p/3041650.html
Copyright © 2020-2023  润新知