Ok if you are like me and you are trying to call a webservice from javascript and you are getting the Web Service is undefined error then this will show you how to solve the problem. I wasted hours trying to figure this out, because all the tutorials and videos show them just adding the service ref to the proxy and it works like magic. 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
<script language="“javascript”" type="“text/javascript”"> function ButtonClick() { MyNamespace.Services.MyService.MyFunction(OnComplete) } function OnComplete(results) { alert (results) } </script>
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.
Share This



THANK YOU THANK YOU THANK YOU!! Couple hours of my life I’ll never get back.
TTHANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU HANK YOU THANK YOU THANK YOU
Yes, I do think your opinion is righteous. (So do lots of people). Luckily majority of people are intelligent :).
Nice…… want to knw how many hours I wasted on this?
Thank you - if only i’d googled earlier, I could have left work by now!
Nice that I could be of service. I searched forever about the problem and finally figured it out after I sat down and thought about it for long time about the differences between the site and app proj.
thank you thank you thank you! You really saved my hours’ crazy researching. I’ve been reading vs’ Help doc, digging in google, but nothing came out until here…
thanks a lot!
HEY!!!
COPIED FROM
http://forums.asp.net/p/1042412/1964351.aspx
Aakash ya because that is me posting it on that forum. I thought I’d make a blog post about it so it’s easier for people to find.
Your post helped me solve the problem I was having. However my experience was slightly different.
I used
myNamespace.myService.myFunction (i.e. Services was unnecessary)
I have no idea why that’s the case.
Also, for completeness, you can also use
[System.Web.Script.Services.ScriptService()] on top of the class rather than:
using System.Web.Script.Services;
and
[ScriptService]
Hi,
I used your method but issue is I am getting namespace undefined error. What is missing?
Thanks,
Vaibhav
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