简体   繁体   English

无法使用Javascript调用ASP.NET中的WebServices

[英]Fail to Call WebServices in ASP.NET in Javascript

I fail to call a webservice. 我无法调用网络服务。 What have I miss? 我想念什么? @@ @@

I am trying to follow this instructions here: How to call an ASP.Net Web Service in javascript 我正在尝试按照以下说明进行操作: 如何使用javascript调用ASP.Net Web服务

And below are my codes, but fails to work. 以下是我的代码,但无法正常工作。

This is my webservice codes: 这是我的网络服务代码:

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!";
        }
    }
}

and this is my ASP.NET page codes: 这是我的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>

both ASP.NET page and Service files are located at the same folder. ASP.NET页和服务文件都位于同一文件夹中。

but when I click on the button. 但是当我点击按钮时。 It says: undefined 它说: 未定义

Have you add web reference to your project? 您是否向项目添加了网络参考? By doing so, the proxy will be created and then you can start consuming the web service methods. 这样,将创建代理,然后您就可以开始使用Web服务方法了。 There's a tutorial you might want to check out. 您可能想查看一本教程

Also check out the topics on MSDN as well. 还可以查看有关MSDN的主题

try making it async: 尝试使其异步:

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

function OnCompleted(data) {
    alert(data);
}

Your method in the web service asmx file says HelloWord but in the markup it says HelloWorld . Web服务asmx文件中的方法说HelloWord但是在标记中说HelloWorld It says undefined because it cannot find the method due to the spelling mistake. 它说undefined,因为由于拼写错误而找不到方法。

If you do not succeed then try the following example: Calling ASMX web services directly from javascript 如果未成功,请尝试以下示例: 直接从javascript调用ASMX Web服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM