1

I'm trying to teach myself AJAX/Web services using C# and javascript. And I think I'm having a namespace issue, judging by the Google-ing I've done.

First of all, my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace SimpleAJAX
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)] 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetServerResponse(string callerName)
        {
            if (callerName == string.Empty)
                throw new Exception("Web Service Exception: invalid argument");

            return string.Format("Service responded to {0} at {1}", callerName, DateTime.Now.ToString());
        }

    }
}

The Web Service.

<head id="Head1" runat="server">
    <title>Web Service call from client-side JavaScript</title>

    <script language="javascript" type="text/javascript">
        function SendRequest() {
            MySampleService.GetServerResponse(form1.MyTextBox.value, OnComplete, OnError, OnTimeOut);
        }

        function OnComplete(arg)
        {
            alert(arg);
        }

        function OnTimeOut(arg)
        {
            alert("timeOut has occured");
        }

        function OnError(arg)
        {
            alert("error has occured: " + arg._message);
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/WebServices/MySampleService.asmx" />
            </Services>
        </asp:ScriptManager>
        <div>
            <input type="text" value="" id="MyTextBox" />
            <input type="button" value="Send Request to the Web Service" id="RequestButton" 
                onclick="return SendRequest()" />
        </div>
    </form>
</body>     

The aspx page.

Taken from (http://www.semenoff.dk/en/Code-Corner/ASP.Net.AJAX/WebService-From-JavaScript.aspx)

And when I run it I get a "'MySampleService' is undefined" error. I followed the tutorial exactly, but obviously I'm still doing something wrong. Little help? Thanks!

3 Answers 3

2

You'll need to include your namespace and your service's actual class name in the call:

SimpleAJAX.WebService1.GetServerResponse(form1.MyTextBox.value, OnComplete, OnError, OnTimeOut);

To see the exact structure of the JavaScript proxy that ASP.NET generates for your ScriptReference, open WebService1.asmx/jsdebug in a browser.

Sign up to request clarification or add additional context in comments.

Comments

1

try this

 function SendRequest() {
            SimpleAJAX.WebService1.GetServerResponse(form1.MyTextBox.value, OnComplete, OnError, OnTimeOut);
        }

Comments

1

You don't have any class called "MySampleService". Your class is named WebService1, so you should use

SimpleAJAX.WebService1.GetServerResponse(form1.MyTextBox.value, OnComplete, OnError, OnTimeOut);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.