‘WebService’ is undefined Error: ASP.NET AJAX javscript call to a web service

Dec 3rd ago

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

Popular Posts

11 Responses to “‘WebService’ is undefined Error: ASP.NET AJAX javscript call to a web service”


  1. 1 jason Posted January 13th, 2008 - 4:19 pm

    THANK YOU THANK YOU THANK YOU!! Couple hours of my life I’ll never get back.

  2. 2 Felipe Blini Posted March 17th, 2008 - 7:49 pm

    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

  3. 3 Jordie Posted April 11th, 2008 - 12:57 am

    Yes, I do think your opinion is righteous. (So do lots of people). Luckily majority of people are intelligent :).

  4. 4 Paul Posted April 16th, 2008 - 5:18 pm

    Nice…… want to knw how many hours I wasted on this?

  5. 5 WayHey Posted April 18th, 2008 - 11:48 am

    Thank you - if only i’d googled earlier, I could have left work by now!

  6. 6 Ryan Posted April 19th, 2008 - 8:33 am

    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.

  7. 7 leonore Posted May 30th, 2008 - 3:13 pm

    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!

  8. 8 Aakash Agarwal Posted June 12th, 2008 - 1:38 am
  9. 9 Ryan Posted June 12th, 2008 - 7:26 pm

    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.

  10. 10 Neil Richards Posted August 2nd, 2008 - 8:11 am

    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]

Who's linking?

  1. 1 Knowledge with Neil » Blog Archive » ‘Webservice’ is undefined effor with ASP.NET AJAX Pingback on Aug 2nd, 2008
    "[...] Credit where credit’s due, thanks to Omen’s blog and Ryan at Solutek. [...] "

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.




  • I'm currently

  • Contact Me via Skype

    Skype Me!
    Skype Me!

     

    December 2007
    M T W T F S S
         
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  

    Categories

     

    Close
    E-mail It