Reputation: 18171
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?
Upvotes: 0
Views: 1101
Reputation: 1
Check the key in web.config, setting mode = "none". This solved my problem.
Upvotes: 0
Reputation: 21
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!
Upvotes: 1
Reputation: 25351
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>
Upvotes: 0