Tuesday, 17 July 2007

ASP.NET AJAX Javscript call - Web Service is undefined Error

I tried to implement this using a script manager and script enabled WS but constantly kept getting the error 'service is undefined'. Talk about infuriating.

I ripped my web.config apart trying to find the problem, hacked my code, moved scripts - alsorts! Searched high and low on the net but could not find the solution to the problem anywhere. I had a sample project that it worked fine in and everything was exactly the same (well almost).

Then i started looking at references. When doing this i spotted the namespace in my project which lit a light bulb. The sample project was a Web Project and there is no namespace property like on the project. So i went back to my javascript and changed the following line as follows:

Webservicename.Webmethod(prams, callback);

to

Namespace.Webservicename.Webmethod(prams, callback);

Worked a treat!

Hopefully this will save someone a days worth of head scratching and wasted time like myself.

47 comments:

J said...

I have been having the same problem, but I must not be wiring it up according to your changes. Still have the "undefined" error.

I have in the aspx file(tags omitted for security):
ServiceReference path="//localhost/myvirtualpath/myServiceName.asmx"
-- and the call:
myWeb.MyServiceClass.HelloWorld("Binky", OnSuccess, OnTimeOut, OnError)

-- in the asmx file:
WebService Language="C#" CodeBehind="~/App_Code/myServiceClassFileName.cs" Class="MyServiceClass"

-- in the webservice class file:
namespace myWeb
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService]

Advice would be greatly appreciated. Am I missing something in the wiring, paths, etc? I don't have many fingernails left.

Omen said...

Hi mate,

I'll take a closer peek as soon as ive cleared a batch of work i need to get done for a deadline. A quick look through your code and i notice you are mising a semi colon from you javascript call.

Is this just a blog typo or have you copied the code?

Omen said...

Also make sure you do a full page refresh (Ctrl-F5) as the js file will probably be cached by the browser. You may be getting the old code still.

If you can create a basic hello world project with the problem with no secret info i will have a look at it for you.

Omen said...

Also - try using the fully qualified path to the script library for the script attribute.

Im sure ive read there was a conflict in this library somewhere.

[System.Web.Script.Services.ScriptService]

Kaz said...

Just a note...I wrestled with this problem for over a week and finally came across you blog. Your suggestion fixed my problem and for that...THANK A BUNCH!!!

Omen said...

No probs Kaz! Its great to be able to help someone. As this is a new blog and not well known like the main MS gurus - its wicked you find it and it helped you out. Makes it all worthwhile.

Malhavoc said...

Thankx a lot! I was getting mad with that :-)

Anonymous said...

Thanks, this saved me a lot of time. Angie

Daniel Williams said...

Thank you thank you thank you! It took me a few hours to find this post, but it is exactly what I needed.

enielsen200 said...

This exact problem happened with PWS 2008 web site Brad Abrams just posted. He included a TopCitiesService WFC service with a blank Namespace (Namespace:=""). The javavscript call resulted in undefined. Based on your comments, I put in a nonblank Namespace, added it to the front of the javascript call and - YAY - success! Thanks.

Anonymous said...

Thanks Omen,

my problem also solved.

thanks a lot

MoLo said...

Thank you!!! I was stuck on this for a couple of hours before I stumbled onto your post, now I can get back to actual work.

Greven said...

This actually didn't solve my problem. But I realised that my project had target framework 2.0, and when I changed it to .NET 3.5, everything worked like a charm. Might help someone as stupid as me..

TIBCO NW Group said...

Omen, I've got to really thank you for this bit of knowledge. My problem was also solved with the namespace issue. I'm new to ASP.NET/AJAX and it's appalling to see how little reference is made to the most basic of the features: Fully qualify your resources. I will post your solution on the www.asp.net/ajax forums. Apparently nobody has come across this problem there, but it's a place where many people go look for an answer. I hope you blog gets more visibility soon.

Thanks for sharing.
-G

Omen said...

No probs - i had another similar namespace issue when attempting to consume a WCF web service in a vb.net winforms app.

Check the blog post i made here about that.

http://omensblog.blogspot.com/2007/06/windows-communication-foundation.html

infocyde said...

Thanks for posting. All the examples I saw left out the namespace for some reason. Interesting. Anyway your post cleared things up. I appreciate it.

Andrew Webb said...

Thanks for the lifesaver.

My sample too had no namespace, so the sample worked but my own code didn't!

Everything has to match: the namespace (present or omitted) in the .asmx file, .cs file, and in the calling JavaScript on the .aspx page.

Changing the namespace in my web service's C# file _didn't_ change the namespace in the .asmx file, which didn't help. Another problem is that it's not that easy to see what's in your .asmx file. Double-clicking on it opens my C# file, whereas double-clicking on the C# file itself doesn't open it! Weird.

Well, once you know, you know.

Thanks again.

Anonymous said...

I got the same error message if I put aspx, asmx and js files under a folder in the web site. Moving all three files, or moving aspx and js files to the root will fix the problem. Be sure to modify the path in the ScriptManager after you move the files around.

Anonymous said...

Another thing to keep in mind is to check that your webservice has no errors... That will also cause the same issue

Hank said...

Here's another "solution"... I've been having the same problem, ripping my hair about it... The call to the WS indeed works, but it keeps returning the OnFail callback from my Javascript call. I have checked namespaces, and rebuilt new services/sites all day just to get somewhere.

I ended up building my WebService and putting it in my local IIS and made a new page that would just return a simple "Hello World" string from the WebService - from the VS.Net 2008 environment - same old "Fail" message box. I then built the site and placed it in my local IIS and it works... Whattehell?! Am I missing something?

Anonymous said...

Many thanks!

Anonymous said...

Thank you Andrew Webb!!!

Your comment saved me.

I had exactly the same problem.

My problem was caused by the class attribute in the ASMX file. (not the code behind!!)

NOTE: when you make a copy of an existing asmx file you have to change the class attribute in your asmx file. Since doubleclicking the file in Visual Studio opens the .asmx.cs file this cannot be done directly from Visual Studio!!!

Anonymous said...

Hi I found few good tutorails on this blog.

you can see them if you like

http://aspnetcsharp4.blogspot.com/2009/05/first-look-vs2010.html
http://aspnetcsharp4.blogspot.com/2009/05/c40-new-features-list.html
http://aspnetcsharp4.blogspot.com/2009/05/variance-in-c-40.html
http://aspnetcsharp4.blogspot.com/2009/05/whats-new-in-aspnet-40.html
http://aspnetcsharp4.blogspot.com/2009/05/aspnet-40-core-services.html
http://aspnetcsharp4.blogspot.com/2009/05/auto-start-web-applications.html
http://aspnetcsharp4.blogspot.com/2009/05/ajax-and-new-template-engine-in.html
http://aspnetcsharp4.blogspot.com/2009/05/shrinking-session-state.html
http://aspnetcsharp4.blogspot.com/2009/05/routing-of-urls-capability-is-added.html

http://aspnetcsharp4.blogspot.com/2009/06/access-twitter-api-using-c.html
http://aspnetcsharp4.blogspot.com/2009/06/free-aspnet40-hosting-available.html
http://aspnetcsharp4.blogspot.com/2009/06/lazy-evaluation-feature-in-c.html
http://aspnetcsharp4.blogspot.com/2009/06/difference-between-shadowing-and.html

http://aspnetcsharp4.blogspot.com/2009/07/microsoft-aspnet-40-data-access.html
http://aspnetcsharp4.blogspot.com/2009/07/microsoft-aspnet-40-whats-next.html
http://aspnetcsharp4.blogspot.com/2009/07/ajax-client-side-templating-in-aspnet.html
http://aspnetcsharp4.blogspot.com/2009/07/jquery-in-aspnet.html
http://aspnetcsharp4.blogspot.com/2009/07/c-interview-questions.html
http://aspnetcsharp4.blogspot.com/2009/07/using-ajax-control-toolbox-with-jquery.html
http://aspnetcsharp4.blogspot.com/2009/07/microsoft-interview-questions.html
http://aspnetcsharp4.blogspot.com/2009/07/json-and-aspnet.html
http://aspnetcsharp4.blogspot.com/2009/07/aspnet-questions-with-detailed-answers.html

Thanks

Willie said...

I have an asp.net 2005 webapplication and i have the same problem. I put a namespace but i can't solve the problem. Any ideas?

Thanks

Gareth said...

Thanks you so much. I waisted a day trying to figure this out!

Aykut said...

Someone said if you have an error in your service, you ll get the same error. Perfectly true.
Mine was putting
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

instead of

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

(I was using sessions)

Anonymous said...

Thanks a lot - you just saved my day :)

Anonymous said...

Thanks a lot! I have been stuck for several days.

Anonymous said...

Encountered the same error (posted code for a "Hello World" application in an Ajax Book - very annoying it didn't mention this).

Worked a treat - thanks for posting this.

Anonymous said...

3 years later and your blog still saved my day. thank you.

Peter H Murton said...

And mine, thanks Peter

Anonymous said...

I love you!

Omen said...

love you too! xx haha :o) Glad to help my friend.

Anonymous said...

I was calling Webservice in a html code usign Service.useService("web url","service name")

This works fine but when I deployed at server it started giving me error, Undefined, I have tried everything.
_
Let me explain little in detailed.

My Service is deployed at another server let suppose www.myservicedomain.com/service.asmx
I have a user website.
www.userwebsite.com

When I deployed my code at userwebsite it says Undefined but If I execute the same code at Localhost, It works fine.
Please your help can save my time.
Thanks in advance

s P i d y said...

I have an error but it is random. It shows and pops up undefined sometimes and sometimes it works just fine. I am using the whole namespace by the way. any ideas?

Me the blogger said...

I am having this issue and still not able to resolve. I created a sample hello world application using C# and it works fine.

When I do the same with VB.NET, it does not see to work. I keep getting the "undefined" error. I have reviewed and compared the code multiple times and they seem to be the same.

Can you provide a sample VB.NET code for the webservice?

Me the blogger said...

I am having this issue and still not able to resolve. I created a sample hello world application using C# and it works fine.

When I do the same with VB.NET, it does not see to work. I keep getting the "undefined" error. I have reviewed and compared the code multiple times and they seem to be the same.

Can you provide a sample VB.NET code for the webservice?

James said...

I didnt have a Namespace in my service so the Namespace i had to use was that of my project.
[ProjectNamespace].[ServiceClass].[MethodToCall]

Thanks!

White Rosa said...

Omen, you are amazing , thank you man , in Arabic "Chokran Jazelan" :) , Asmaa.

shahram said...

Add EnablePageMethods=”true” for ScriptManager

Anonymous said...

Thanks dude.. u saved my day :)

Anonymous said...

You saved me from pulling out what little hair I have left!
Thanks a ton. Cheers

Markeins said...

Thank you and also shahram for his last comment. I did everything that was mentioned here but don't know why required to set EnablePageMethods="true" in ScriptManager. Perhaps it's because I'm using MasterPage and ScriptManagerProxy in ContentPlaceHolder??

Anonymous said...

Thanks it worked

Anonymous said...

Hi Omen,
thanks for the namespace hint, it solved my issue.

Regards
R

Anonymous said...

Thanks mate!
Adding namespace worked like charm.

sumit said...

thanks for help