【问题标题】:Asmx webservice referencing issueAsmx Web服务引用问题
【发布时间】:2011-06-11 06:54:06
【问题描述】:

我需要在我的客户端页面中使用 JavaScript 调用我的 Web 服务方法。我想我没有正确地引用这个,希望能帮助解决这个问题。

错误消息显示“CalendarHandler 未定义”

<%@ WebService Language="C#" CodeBehind="~/App_Code/CalendarHandler.cs"
  Class="CalendarHandler" %>

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master"
  AutoEventWireup="true" CodeFile="CalendarPage.aspx.cs" Inherits="CalendarPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">

      <input type="button" id="loadevents" onclick="callLoadEvents();" />
      <div id="eventresults"> </div>
      <div id="resultFailed"> </div>

      <script language="javascript" type="text/javascript">
            var tasks;

            function callLoadEvents() {

                  Speak.CalendarHandler.GetEvents(GetLoadAddress_success, OnFailed); 
            }
            function GetLoadAddress_success(e) { 
                  tasks = e;
            }
            // --------------------------
            function OnFailed() { 
                  $get('resultFailed').innerHTML = "failed";
            }

      </script>
</asp:Content>



using System.Web;
using System.Web.Services;

[WebService(Namespace = "Speak")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.Web.Script.Services.ScriptService]
[System.ComponentModel.ToolboxItem(false)] 
public class CalendarHandler : System.Web.Services.WebService 
{
      static IDictionary<DateTime, String> Calendarevents;//hold my events in this 

    public CalendarHandler () {
          Calendarevents = new Dictionary<DateTime, string>();
          Calendarevents.Add(DateTime.Now, "Going to meeting with XYZ Company");
          Calendarevents.Add(DateTime.Now.AddDays(1), "XML Class at 2pm");
          Calendarevents.Add(DateTime.Now.AddDays(1),"ASP.NET 3.5 Ajax");
          Calendarevents.Add(DateTime.Now.AddDays(1),"Pocket Guide");
          Calendarevents.Add(DateTime.Now.AddDays(1),"Grocery Shopping");

    }

    [WebMethod]
    public IDictionary<DateTime, String> GetEvents()
    {
        return Calendarevents;
    }

}

感谢您的帮助

【问题讨论】:

    标签: javascript asp.net ajax web-services asmx


    【解决方案1】:

    您需要将您在 webmethod 中的集合序列化为 json,并让 return 方法返回一个字符串(= 实际上是您的集合的序列化输出 = json)。

    看看Encosia。它可能有关于 asp.net、javascript 和 jquery 一起工作的最彻底的文章。

    using System.Web.Script.Serialization;
    
    
     JavaScriptSerializer jss = new JavaScriptSerializer();
     return jss.Serialize(CalendarEvents);
    

    我宁愿用jquery来调用webservices。并注意 d。 asmx 中的前缀。使用 jquery 使用它们时的 web 服务。

    【讨论】:

    • 感谢指正。那么您也可以帮助解决参考问题吗?另外,我不能为此使用 JQuery,因为它现在是不允许的。
    • 您也可以使用默认的 asp.net ajax 脚本库。它也可以,但我认为 jQuery 在进行 ajax 调用时语法更流畅(= 编写更少)。
    • 或者您可以使用页面方法,然后完全取消使用 Web 服务。 singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
    • 感谢您对我的网站的好评。
    【解决方案2】:

    你在那个页面的 Master 上有一个 ScriptManager 吗?

    启用漂亮的Namespace.Service.Method 语法基本上有两个关键:MicrosoftAjax.js 和特定于服务的 JavaScript 代理。

    当您在页面上的任何位置(或者它是 Master(s))有一个 ScriptManager 时,您会自动获得 MicrosoftAjax.js。

    要获取 JavaScript 代理,您需要 ScriptManager 上的 ServiceReference 或指向生成源位置的 JavaScript 包含。例如:

    <asp:ScriptManagerProxy>
      <Services>
        <asp:ServiceReference Path="/CalendarHandler.asmx" />
      </Services>
    </asp:ScriptManagerProxy>
    
    <script>
      // Speak.CalendarHandler.GetEvents() will be available here.
    </script>    
    

    实际上所做的只是注入/CalendarHandler.asmx/js(或在调试中运行时/jsdebug)上可用的脚本。这是服务上的ScriptService 属性启用的一部分。

    因此,只要 MicrosoftAjax.js 已经从页面上的某个位置注入,您也可以自己简单地添加对该 JavaScript 代理的脚本引用:

    <script src="/CalendarHandler.asmx/js"></script>
    
    <script>
      // Speak.CalendarHandler.GetEvents() will be available here.
    </script>
    

    更新:

    如果服务代理生成代码不遵守 [WebService] 属性中的 Namespace 参数,您可以使用 namespace 关键字来命名服务代码吗?

    namespace Speak
    {
      [ScriptService]
      public class CalendarHandler : WebService
      {
        [WebMethod]
        public IDictionary<DateTime, string> GetEvents()
        {
          // Etc.
        }
      }
    }
    

    我可以确认这种方法确实会影响生成的服务代理。

    【讨论】:

    • 我从母版页脚本管理器中删除了服务引用,而是在子页面中使用了上面的脚本管理器代理。我仍然无法使用我为我的服务定义的命名空间调用 webservice 方法。
    • 我可以使用 CalendarHandler.GetEvents(GetLoadAddress_success, OnFailed); 调用我的参考。但不是 Speak.CalendarHandler.GetEvents(GetLoadAddress_success, OnFailed);
    • 谢谢。我删除了 [Webservice] 属性并使用了命名空间参数,效果很好。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多