0

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

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:

<%@ 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.

but when I click on the button. It says: undefined

Community
  • 1
  • 1
mjb
  • 7,649
  • 8
  • 44
  • 60

3 Answers3

1

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. There's a tutorial you might want to check out.

Also check out the topics on MSDN as well.

woodykiddy
  • 6,074
  • 16
  • 59
  • 100
1

try making it async:

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

function OnCompleted(data) {
    alert(data);
}
Zain Shaikh
  • 6,013
  • 6
  • 41
  • 66
-1

Your method in the web service asmx file says HelloWord but in the markup it says HelloWorld. It says undefined because it cannot find the method due to the spelling mistake.

If you do not succeed then try the following example: Calling ASMX web services directly from javascript

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27