‘Webservice’ is undefined effor with ASP.NET AJAX
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.
November 9th, 2008 at 7:27 pm
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
January 13th, 2009 at 6:48 pm
[System.Web.Script.Services.ScriptService()]
add this line above your webservice class definiton
March 10th, 2009 at 3:43 am
“aokocax” – Thank you buddy! All fixed now!
Cheers