Microsoft JScript runtime error: 'WebService' is undefined ??

Hey all, ive been using CTP Atlas for a while, and im tying to upgrade some of my code to beta 1..

but im getting: Microsoft JScript runtime error: ‘WebService’ is undefined

New project > Ajax Enabled Website

WebService.asmx.js
function test()
{
    WebService.HelloWorld();
}

WebService.asmx
[WebMethod]
public void HelloWorld()
{
    string test=  "Hello World";  //added a breakpoint here to catch the call
}

Default.aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
       <asp:ServiceReference Path="WebService.asmx" />
    </Services>
    <Scripts>
 <asp:ScriptReference Path="WebService.asmx.js" />
    </Scripts>
</asp:ScriptManager>
<div>
     <button type=button onclick="test();" >hahaha</button>
</div>

 

thats the basics ive done, now it should work….. but it doesnt :-(

Any idea waht im doing wrong?? Cheers guys!

 

 

I had the same issue, but found that the WebService.asmx class required a new attribute.  Once I added it, all worked:

[Microsoft.Web.Script.Services.ScriptService()]
public class WebService : System.Web.Services.WebService
{
 [WebMethod]
 public void HelloWorld()
 {
     string test=  "Hello World";  //added a breakpoint here to catch the call
 }
}

Hope this helps. 

 

steven 

 

Folks,

I am having the same problem. Adding the scriptservice attribute does not help.

 //Web Service

Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports Microsoft.Web.Script.Services

 

<WebService(Namespace:="http://tempuri.org/")> _

<ScriptService()> _

Public Class CarService

Inherits System.Web.Services.WebService

<WebMethod()> _

Public Function getcarvalue(ByVal strCarMake
As String,
ByVal strCarModel
As String,
ByVal strCaryear
As String)
As Integer

……..

End Function

End Class

//Client Code

<script language="javascript"
type="text/javascript">

// <!CDATA[

function Button1_onclick() {

requestValue = CarService.getcarvalue(

form1.txtMake.value,

form1.txtModel.value,

form1.txtYear.value,

OnComplete,

OnTimeOut);

return false;

}

function OnComplete(result)

{

alert("Car is worth: $" + result);

}

function OnTimeOut(result)

{

alert("Timeout: " + result);

}

 

// ]]>

</script>

</head>

<body>

<form
id="form1"
runat="server">

<div>

 

<asp:ScriptManager
ID="ScriptManager1"
runat="server">

<Services>

<asp:ServiceReference
Path="CarService.asmx"/>

</Services>

</asp:ScriptManager>

 

Make: &nbsp;

<input
id="txtMake"
type="text"
/>

<br
/>

Model:&nbsp;

<input
id="txtModel"
type="text"
/><br
/>

Year: &nbsp;&nbsp;

<input
id="txtYear"
type="text"
/>

<br
/>

<input
id="Button1"
type="button"
value="Get Value"
onclick="return Button1_onclick()"
/></div>

</form>

</body>

</html>

 

 

I’m also having the same problem.  It would seem that someone from Microsoft would explain the bugs they create.  If anyone could figure out how to fix this problem, please let us know.

 Thanks,

Michael.

Hi kali.mist,

I tested your code against Beta 2 and it worked just fine.

Maíra

Thanks Maira,

I rebuilt everything from scratch and it worked!! Ive compared the two web sites but could not find the problem that caused the first to fail.

Cheers

Mick

 

In your case Kryptonic, the first thing to do would be to add the [Microsoft.Web.Script.Services.ScriptService()] to the web service.

Another thing that I noticed is that your javascript function doesn’t have a callback function. You need to add something like this:
function test() {
WebService.HelloWorld(OnRequestComplete);
}

// Event handler that processes the
// Web service return value.
function OnRequestComplete(result)
{
alert(result);
}

Make sure that you rebuild your solution, in order to update the generated proxy file for your web service.
You can check the proxy file, typing /js after your service address (e.g. http://yourapppath/yourservicename.asmx/js)

I also would use another name for the web service just to avoid collision with the WebService attribute. 

And the last thing (I guess)… I would change the way you include your js file. The ScriptReference control is meant to be used differently.
So I would place the script code inside the page or would add trough a 
<
script type="text/javascript"
src="WebService.js"
/>

Hope that helps,

Maíra

Hi

 I came across this page lookig for the web service undefined error problem.  I eventually found the problem which was namespace related.  Ive detailed it in my blog:

http://omensblog.blogspot.com/2007/07/aspnet-ajax-web-service-calls-from.html

cheers

Damian

 

The problem lies when you are using a web application project. Since web application projects use namespaces you need to add it before your call to the service. Also I needed to add
using System.Web.Script.Services; and [ScriptService] to my asmx code behind. Below I will give an example of how to get this working.

Create your webservice. I added mine in a sub directory of my site named Services. So when looking into the code behind of the webservice you will see something like:
namespace MyNamespace.Services you want to remember this. Next thing is to add:
using System.Web.Script.Services; with the rest of the includes and also add the line:
[ScriptService] just before the public class MyService : System.Web.Services.WebService

Next is to go to the aspx page and create the proxy.

  

<asp:scriptmanagerproxy id="ScriptManagerProxy" runat="server">
	<services>
		<asp:servicereference path="~/Services/MyService.asmx"></asp:servicereference>
	</services>
</asp:scriptmanagerproxy>
 
Next comes the javascript

<div class=”codecolorer-container javascript”><div class=”codecolorer” style=”font-family: monospace;”><script language=”javascript” type=”text/javascript”></div><div class=”codecolorer” style=”font-family: monospace;”> </div><div class=”codecolorer” style=”font-family: monospace;”>    function ButtonClick()
        { MyNamespace.Services.MyService.MyFunction(OnComplete) }
</div><div class=”codecolorer” style=”font-family: monospace;”> </div><div class=”codecolorer” style=”font-family: monospace;”>    function OnComplete(results)
        { alert (results) }
</div><div class=”codecolorer” style=”font-family: monospace;”> </div><div class=”codecolorer” style=”font-family: monospace;”></script> </div></div>

Notice the “MyNamespace.Services.” before the name of the webservice. This is important to add when using a web application project, it is not need when making a regular
web site because a web site does not use namespaces. So there you go, you should now be able to get things up and running.

 

Hi Lankern,

I am also using a Web Application prject and I have added a namespace to the .asmx class. However my code is calling the js page but gets an error on the line where i make the call to the WebService. I have been circling this issue for 2 days, any help is
greatly appreciated.

<asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="WebService.asmx" />
        </Services>
        <Scripts>
            <asp:ScriptReference Path="WebService.js" />
        </Scripts>
    </asp:ScriptManager>

 

// JScript File
function HelloWorld(url) {
   Namespace.Services.WebService.HelloWorld(url,OnSucceeded);
}
function OnSucceeded(result, userContext) {
   alert(result);
}


if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

 

Hi andyaiyer,

What is the error message you’re getting?

Do you have the
ScriptService
attribute applied to your Web service class?

Regards,

Maíra

Maira,

 Thank you for responding to my issue. Please allow me to explain in more detail what I am trying to do. I have 2 Web Application projects, they are mutually remote to each other. Let us call them ServerProjA and ClientProjB.  I am trying to call a web service
which is on the remote Server (ServerProjA) using AJAX ASP.Net on the client project (ClientProjB). Since ASP.Net does not allow you to call a Remote Web Service using AJAS ASP.Net… I have added a Web Service (ClientWS) to my client project (which is a wrapper
to my remote Web Service) .

I was getting an error when i was trying to access the Web Service (ClientWS) from within my JScript on the Client Javascript page.

However now upon further debugging….when I tried to directly access the Client Web service
http://localhost/ClientProjB/ClientWS.asmx (for example) and invoke a Web Method. Please note that this web service will dynamically call the Web Service on ServerProjA, I am passing the web service URL
dynamically (http://localhost/ServerProjB/ServerWS.asmx ) I get the following error

System.NotSupportedException: The URI prefix is not recognized.
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at System.Net.WebRequest.Create(Uri requestUri)
   at System.Web.Services.Protocols.WebClientProtocol.GetWebRequest(Uri uri)
   at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebRequest(Uri uri)
   at System.Web.Services.Protocols.SoapHttpClientProtocol.GetWebRequest(Uri uri)
   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
   at FG9State.RegionalWSProxies.Mandates.RegisterMandates.HelloWorld() in C:WorkFitnessGramHK CoreHK Core 1.0FG9StateBOLRegisterMandates.vb:line 63
   at FG9State.StateMandates.HelloWorld(String WebServiceURL) in C:WorkFitnessGramHK CoreHK Core 1.0FG9StateStateMandates.asmx.vb:line 18
The following is the  Web Service (ClientWS) in my client project (which is a wrapper to my remote Web Service) .
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Services


<WebService(Namespace:="Microsoft.Samples.XmlMessaging.WebServices")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1, EmitConformanceClaims:=True)> _
<ScriptService()> _
Public Class StateWrapper
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld(ByVal WebServiceURL As String) As String
        Dim strResult As String
        Dim WS As New NameSpace.Services.WebService() ' Calling the remote Web Service here
        WS.Url = WebServiceURL			'Pasing the URL of the location of the web service.
        strResult = WS.HelloWorld()
        Return strResult
    End Function
End Class

  My ServerWebService tests okay when i invoke the Service directly in a Browser …and is a simple implementation.

WebService(Namespace:="Microsoft.Samples.XmlMessaging.WebServices")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1, EmitConformanceClaims:=True)> _
<ScriptService()> _
Public Class WebService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return
"Hello World"
    End Function
End Class

 

I dont understand why i cannot get the 2 web services to talk to each other? If i can achieve this first then i can call my first web service via AJAX. If you have questions please feel free to indulge me, I am tried a lot of different things from changing
namespaces to client side debugging etc. One more note is that I am using Web Application Projects and not Web site project.

 your help is very appreciated.

Apparently the problem you’re having is not in Ajax, but in the communication between the two Web services.
I would do a couple of things to isolate the problems:
- I would make the wrapper just return a string and nothing else, just to make sure that the Ajax part is working in your Web application;
- I would create a test page and call the web service from the remote server in the traditional way to make sure that your proxy configuration is OK;

Hope this helps,

Maíra

Maira,

Thank you for your help 

Please consider this issue closed. I have documented the problem, steps and solution here for whoever is reviewing my thread. Please remember that i was using Web Application Project in my solution (not Web site).

1. I had to do a clean and rebuild after which my Web Service from ProjA was able to communicate with web service in ProjB.

2. I did not find a successful documented way to do consume a Web Service hosted on a remote domain via AJAX. The only article I found was to create a wrapper Web Service in your project which makes calls to the Remote WS and then you can AJAX call the web
service wrapper in your project using Script Manager , Services, Scripts etc. However i kept getting the ‘Webservice is undefined’ JScript error. I still wish if someone has an implementation or solution that they can share,

3. What i ended up doing is to make the web service call as a HTTP request to the remote domain directly from another form. The flow is as follows Button Click-> Java Script–> Call a Form1.aspx, and in Form1 codebehind make the call to Remote Webservice
and return the data back.

 

I have only been able to call Web Services from JS when the Project type was a "Web Application".  When trying this in a "Web Site" solution in Visual Studio, it never works.

 Can anyone confirm that the project needs to be a "Web Application" vs adding a web service to your web site and trying this?

 Edit : Ignore my post.  I figured out the Namespace issue afterward.

I’m also getting this issue but even when I add the namespace it doesn’t fix it. I have a web application with the namespace MyWebApp and the web service is in this project. When I just call MyWebService.ReturnResults…. I get a message saying ‘MyWebService
is undefined’. When I prefix with the namespace I then get a message saying ‘MyWebApp is undefined. The call to the web service is actually made from an ASP.Net AJAX control which is in another project with a different namespace. Could this be causing my issue?
My web service starts as below… 

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

    public class MyWebService : System.Web.Services.WebService
    {
      [WebMethod]
      public DataTable ReturnResults(string searchParametersJSONstring)
      {
        ....
      }
    }
}
Thanks

hi All,

 iam using java script  to Test  method execution time. the method  written in webservice.its running perfectly but iam getting this error message "Microsoft JScript runtime error: ‘debug’ is undefined".

can  anbody help me.

Default.aspx

function TestTimeout()

{

 

debug.trace("–Start–");TestService.set_defaultFailedCallback(
function(result, userContext, methodName)

{

var timedOut = result.get_timedOut();

if( timedOut )

debug.trace( "Timedout: " + methodName );

else

debug.trace( "Error: " + methodName );

});

TestService.set_defaultSucceededCallback( function(result)

{

debug.trace( result );

});

 

TestService.set_timeout(5000);

TestService.HelloWorld("Call 1");

TestService.Timeout("Call 2");

TestService.Timeout("Call 3");

TestService.HelloWorld("Call 4");

TestService.HelloWorld("Call 5");TestService.HelloWorld(null);

 

}

 

Hi All,

I also had the same problem (Microsoft JScript runtime error: ‘WebService’ is undefined). It’s all about a Namespace Issue :). But now the problem is web service doesn’t return the value i wand when it is called within the javascript. Instead it’s saying
"Undefined"

[WebMethod()]

[ScriptMethod()]

public
string HelloWorld()

{

return
"Hello World";

}

Ok I have been racking my brain out on this searching loads of pages for ours trying to find a solution. I have finally found what fixed it for me, however it seems there are loads of solutions scattered all over the internet that have worked for others
so no idea if this is the one.

I found that I needed ot create functions to deal with the respond of my web service in JavaScript, calling the web service name and passing it directly into a function is not enough, you need to specify the name of the functions in the parameters when calling
a WS in JavaScript like this:

<script type="text/javascript">

    NameSpaceName.WebServiceName.HelloWorld(OnSuccess, OnFail);


    function OnSuccess(asd) {
        alert("Success: " + asd);
    }

    function OnFail(asd) {
        alert("Fail: " + asd);
    }

</script>

If your WebService accepts a parameter it will look like this:

<script type="text/javascript">

    var paramter1 = "The parameter contents"
    NameSpaceName.WebServiceName.HelloWorld(parameter1, OnSuccess, OnFail);


    function OnSuccess(asd) {
        alert("Success: " + asd);
    }

    function OnFail(asd) {
        alert("Fail: " + asd);
    }

</script>

I am assuming that you just add as many parameters as you need… I havn’t tested it with more than one though.

 

Hope this helps save some people some time! It certainly took me a while to figure it out.

 

Leave a Reply