0

I have simple web service:

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

    namespace fmNVBwebSrv
    {
        /// <summary>
        /// Summary description for fm
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        [System.Web.Script.Services.ScriptService]
        public class fm : System.Web.Services.WebService
        {

            [WebMethod]
            public string HelloWorld(string callerName)
            {
                return "Hello World";
            }
        }
    }

I'm trying to call it from javaScript:

<head runat="server">
<title>Web Service call from client-side JavaScript</title>
<script language="javascript" type="text/javascript">
function SendRequest() 
{
    fm.HelloWorld(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="http://localhost:55661/fm.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>

I got error in console Uncaught ReferenceError: fm is not defined. I'm new in Java Script. What is missing there?

4
  • Possible duplicate of How to call a webservice method from an html page [javascript] with out refresh the page Commented Nov 2, 2015 at 16:24
  • @haraman That's not a good duplicate, because that question was not for ASP.Net. Although the answer also works for AS.Net, the OP is probably more interested in the ASP.Net way of calling a webservice. Commented Nov 2, 2015 at 16:48
  • I think the web method needs to be static. Commented Nov 2, 2015 at 17:00
  • @JohnPaul No it doesn't need to be static. See the link in my answer for a full example from Microsoft. Commented Nov 2, 2015 at 19:45

3 Answers 3

1

I had the same problem and solved it. You need to add namespace when you call the method:

function SendRequest() 
{
    fmNVBwebSrv.fm.HelloWorld(form1.MyTextBox.value, OnComplete, OnError,
    OnTimeOut);
}

Hope this will help!

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

Comments

0

You cannot call fm directly from JavaScript like that, because JavaScript knows nothing about your webservice, so you will have to tell JavaScript what to do. Check this Microsoft page for a walkthrough. Try this:

<head runat="server">
<title>Web Service call from client-side JavaScript</title>
<script language="javascript" type="text/javascript">
var helloWorldProxy;

// Initializes global and proxy default variables.
function pageLoad()
{
    // Instantiate the service proxy.
    helloWorldProxy = new fmNVBwebSrv.fm();

    // Set the default call back functions.
    helloWorldProxy.set_defaultSucceededCallback(SucceededCallback);
    helloWorldProxy.set_defaultFailedCallback(FailedCallback);
}


// Processes the button click and calls
// the service Greetings method.  
function SendRequest()
{
    var HelloWorld = helloWorldProxy.HelloWorld();
}

// Callback function that
// processes the service return value.
function SucceededCallback(result)
{
    var RsltElem = document.getElementById("Results");
    RsltElem.innerHTML = result;
}

// Callback function invoked when a call to 
// the  service methods fails.
function FailedCallback(error, userContext, methodName) 
{
    if(error !== null) 
    {
        var RsltElem = document.getElementById("Results");

        RsltElem.innerHTML = "An error occurred: " + 
            error.get_message();
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://localhost:55661/fm.asmx" />
</Services>
</asp:ScriptManager>
<div>
<input type="text" value="" id="MyTextBox" />
<input type="button" value="Send Request to the Web Service" 
       id="RequestButton" onclick="SendRequest()" />
</div>
</form>
<script language="javascript" type="text/javascript">
pageLoad();
</script>
</body>

It this code above, I included the JavaScript in the aspx page and called the pageLoad() function at the end. I did that just to keep it the way you had it in your question. However, it is probably better to follow the MSDN example by saving the JavaScript file into a HelloWorld.js file and then reference it in your aspx file like this:

<asp:ScriptManager runat="server" ID="scriptManager">
    <Services>
        <asp:ServiceReference path="~/fm.asmx" />
    </Services>
    <Scripts>
        <asp:ScriptReference Path="~/HelloWorld.js" />
    </Scripts>
</asp:ScriptManager>

4 Comments

I got error Uncaught "TypeError: Cannot read property 'HelloWorld' of undefined" in line var HelloWorld = helloWorldProxy.HelloWorld();
Perhaps you need to save the JavaScript into a .js file and add <Scripts><asp:ScriptReference Path="~/HelloWorld.js" /></Scripts> to your <asp:ScriptManager> like the example from MSDN. Alternatively, you probably can call the pageLoad() manually. I will update my answer to show that.
Now I have error Uncaught "ReferenceError: fmNVBwebSrv is not defined" in line "helloWorldProxy = new fmNVBwebSrv.fm();"
Like I said, you probably better save the script to a .js file like the MSDN example. Too many things to think about, so it is not easy to make the script work in the header like you want. If you still want to try, maybe it will work if you move it to the end of the page and remove my call to the pageLoad() function.
0

Check the key in web.config, setting mode = "none". This solved my problem.

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.