【问题标题】:Fail to Call WebServices in ASP.NET in Javascript在 Javascript 中调用 ASP.NET 中的 WebServices 失败
【发布时间】:2013-06-03 06:55:38
【问题描述】:

我未能调用网络服务。我错过了什么? @@

我正在尝试按照以下说明操作:How to call an ASP.Net Web Service in javascript

以下是我的代码,但无法正常工作。

这是我的网络服务代码:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

namespace Library
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService()]
    public class ReleaseSessionObject : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWord()
        {
            return "Hellow Word!";
        }
    }
}

这是我的 ASP.NET 页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="Library.WebForm6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>
    <form id="form1" runat="server">
    <div>

    <script src="ReleaseSessionObject.asmx" type="text/javascript"></script>
    <script type="text/javascript">
        function HelloWorld() {
            try {
                var a = Library.ReleaseSessionObject.HelloWorld();
                alert(a);
            }
            catch (err) {
                alert(err.Message); 
            }
        };
    </script>

    <asp:ScriptManager ID="ScriptManager1" runat="server">
      <Services>
        <asp:ServiceReference Path="~/ReleaseSessionObject.asmx" />
      </Services>
    </asp:ScriptManager>

    <input type="button" onclick="HelloWorld();" value="Click This" />

    </div>
    </form>
</body>
</html>

ASP.NET 页面和服务文件都位于同一个文件夹中。

但是当我点击按钮时。它说:未定义

【问题讨论】:

    标签: c# javascript asp.net web-services


    【解决方案1】:

    您是否为您的项目添加了网络参考?通过这样做,将创建代理,然后您可以开始使用 Web 服务方法。您可能想查看tutorial

    还可以查看 MSDN 上的 topics

    【讨论】:

      【解决方案2】:

      尝试使其异步:

      function HelloWorld() {
          Library.ReleaseSessionObject.HelloWorld(OnCompleted);
      }
      
      function OnCompleted(data) {
          alert(data);
      }
      

      【讨论】:

        【解决方案3】:

        您在 Web 服务 asmx 文件中的方法是 HelloWord,但在标记中却是 HelloWorld。它说未定义,因为拼写错误找不到方法。

        如果您没有成功,请尝试以下示例: Calling ASMX web services directly from javascript

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多