Web development has been moving towards faster and quicker deployment. Popular frameworks such as .NET 2.0 and Prototype.js allow for quick AJAX and web services development, but are rarely used together. Web services made with ASP.NET are usually used along-side with ASP.NET AJAX on ASP pages. However, if your site does not use ASP but you would still like to consume your ASP.NET web services with just JavaScript, then read on.

First, create your ASP.NET web service. Once you are finished, visit the Web.Config file and enter the following under the <system.web> node:

<webservices>
    <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
    </protocols>
</webservices>

After that’s done with, view your web service’s .asmx file in a web browser. Click the name of a web service operation and you’ll be taken to the operation’s Invoke page. Scroll to the bottom and you can clearly see how to structure your header for calling your web service method using POST or GET:
ASP.NET Web Services Invoke Page

You can now use Prototype or another JavaScript method to call the web service. An example AJAX call using POST with Prototype.js could be:

new Ajax.Request('/myws.asmx/HelloWorld',
    {
        method: 'post',
        postBody: 'strName=David',
        onSuccess: function(transport){
          var response = transport.responseText || "no response text";
          alert("Success! nn" + response);
        },
        onFailure: function(){ alert('Something went wrong...') }
    }

5 Responses to “Consuming ASP.NET web services with Prototype.js”

hi
i use this method. but i have problem in the iis.
in the iis i get a error that say ” the ‘/myws.asmx/HelloWorld’” dont find.
pls help me
tanks

Comment by muhamad — January 21, 2008 @ 7:46 am

i need de example with “parameter”
thank you!
Emiilano from Argentina

Comment by Emiliano — February 27, 2008 @ 9:48 am

Sounds great! Your blog is one of my most favorite now ;) . You have hit the nail on the head, just like you always do.

Comment by Raju — April 10, 2008 @ 11:13 pm

After reading through this article, I feel that I need more information on the topic. Could you suggest some more resources please?

Comment by Samuel L. — April 21, 2009 @ 11:42 pm

Very well said, will retweet this one for you :)

Comment by Simon Slade — April 1, 2010 @ 4:36 am

leave a reply?