I came across an annoying problem when playing with the ASP.NET Ajax extensions earlier today. I was trying to call a script-enabled web-service but kept getting a javascript error telling me my service was undefined. It’s been a month or so since I’ve used MS Ajax and needed a refresher.
Some background:
- My project namespace is ajaxTest
- My webservice class is helloTest
- My webservice function is helloWorld
I enabled the web service using the [System.Web.Script.Services.ScriptService()] attribute
I put the script manager code block in place and included a reference to the service.
<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
<Services>
<asp:ServiceReference Path=”helloTest.asmx” />
</Services>
</asp:ScriptManager>
I then created some javascript to call the web service
function testScriptService()
{
var tmp = helloTest.helloWorld(onResult, onTimeout, onError);
}
It was at this point I received the “helloTest is undefined” message.
A quick search on google revealed two websites which helped me. The first had the right answer (in my situation). Strangely, the second website had a slightly different solution which seems to have helped a number of people, but I was unable to figure out why (I didn’t work for me).
So, the solution that worked:
You need to fully qualify the javascript with the namespace of your webservice.
Namespace.Class.Function >> ajaxTest.helloTest.helloWorld
The solution which didn’t work for me but has worked for others is to use the following structure:
Namespace.Services.Class.Function >> ajaxTest.Services.helloTest.helloWorld
No idea why the difference, but hey.
Credit where credit’s due, thanks to Omen’s blog and Ryan at Solutek.
This drove me crazy all day. I had no intellisense with regard to the web services, kept getting “object undefined error”, etc. I rebuilt as a website, not a project. This created slightly different directory structure (automatic app_code directory, for example). It also didn’t create the “web application 1” sub folder. For whatever reason, it worked. I think it was related somehow to the way the asmx file points to the class file — somehow one couldn’t see the other. Now I have intellisense, everything working fine.
Wrox has a good example of it in chapter 10 of their book, the source code can be downloaded at:
http://www.wrox.com/WileyCDA/WroxTitle/Beginning-ASP-NET-3-5-In-C-and-VB.productCd-047018759X,descCd-DOWNLOAD.html
[System.Web.Script.Services.ScriptService()]
add this line above your webservice class definiton
“aokocax” – Thank you buddy! All fixed now!
Cheers
Thanks for taking the time … this helped me.
I just had this problem and thought I’d post it out there so people who are banging their heads against their desk can hopefully solve their problem.
Everything worked in DEV (.Net 4, IIS7) but when I moved it to my company’s production env, i started getting this “Undefined” error.
I’m going to list all the things I tried because it’s a compilation of 12 hours of digging through google and maybe one of the below will help your problem.
Browser
I’m using IE9, and I tried every “Compatibility Mode” it had under Dev Tools (F12). I also tried adding the production site to my Trusted Sites and Intranet Sites. I also lowered my security settings as low as they would go and still nothing.
I also tried Firefox 4 and Chrome – neither one worked with the production site, but did work with my DEV site.
WebService.asmx
I played a thousand variations here. I tried everything from changing “Namespace := “myWSNamespace” to blank, my webservice’s URL, to wrapping the class in a Namespace.
Some sites suggested you had to specify the namespace because it’s part of how you reference the service in your javascript code. Didn’t work for me.
_
_
_
Public Class myWSClass
Inherits System.Web.Services.WebService
_
Public Function isActive() As String
Return “True”
End Function
Script Manager
I used the following scriptmanager. It didn’t matter if the webservice was in a folder called “services” on the root or in the same folder as the javascript code. It did NOT work.
All folder and subfolders had ASPNET user account with FULL permissions. Still nothing.
Javascript
Finally, the problem child. The following ExecuteProcess() function is called by an HTML button. I played for hours with the namespaces.
Some sites say you have to follow the following format: Namespace.Class.Function()
I mix and matched namespaces, class names and then some. In the end what worked was Class.Function(). Not sure if this is a .Net 4 thing (versus a previous .Net framework), but that’s what worked for me.
function ExecuteProcess() {
myWSClass.isActive(SucceededCallback);
} //End Function
function SucceededCallback(result) {
var strWSResults = result;
} //End Function
But it still didn’t work in production! So here are two more things I did that eventually got it to work:
Upon closer look at the “View Source” I noticed this line:
This means the browser was linking to the service via the following URL: http://yourdomain.com/myWSClass.asmx/jsdebug The URL downloads a JS file that Javascript uses to talk to make calls to the webservice.
When i went directly to the URL, my devbox prompted me to download a JS file, but in production it wasn’t, instead it was generating some error message.
I researched the “jsdebug error” and found this article at stackoverflow.com
http://stackoverflow.com/questions/5382239/request-for-wstestservice-asmx-jsdebug-returns-500-error-on-server-fine-in-devel
The poster was able to solve the problem by re-registering .Net 4:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>aspnet_regiis -i
OK! This ALMOST got me there. I found another article that clued me into adding FULL namespaces when calling assemblies in webservices.
So even though my webservice code had “IMPORTS SYSTEM.DATA” and was compiling correctly, it was still erroring in production. Not sure why – but i went ahead and changed all my
ds = New DataSet
to
ds = New System.Data.DataSet
I added full namespaces to ALLLLL the different places in the webservice and FINALLY it all works in production.
Again, not sure which one specific thing I tried fixed it, but hopefully this will help you.
Carlos
omg, finally! Thanks for the solution. In .Net 4.0 fully qualifying the web service isn’t required in JS, but when I moved my project down to 3.5 to match production, sure enough this exact problem reared it’s head. Thanks again.
Pingback: Proxy Servers » WCF Ajax Service Not Found Due to WebBinding Issue
thanks “aokocax” you saved my precious time. you rocks