Click here to Skip to main content
14,302,993 members

Invoke ASP.NET Web service from javascript without proxy

Rate this:
3.00 (4 votes)
Please Sign up or sign in to vote.
3.00 (4 votes)
8 Apr 2009CPOL
This sample demonstrates how to call an external Ajax web service without creating code behind proxy from JavaScript


Traditionally when we consume a web service, we create a proxy of the by adding a web reference thru Visual studio. This proxy class contains web service exposed methods and these

methods can be invoked from code behind.This works fine if web service path does not change.

Secondly if new functionality is exposed from the web service, we need to change the update the web reference and proxy will be updated accordingly.

At some places where we need to populate data dynamically based on Database driven configuarations like integrating various systems in ERP applications, and if we do code change

in highly configurable environments, the overall efforts would be exceede if we do static code change and further cost of customization would be unexpected high. 

We can call an Ajax web service easily by using Sys.Net.WebServiceProxy to invoke the

Web service from Javascript, just we need to add Script Manager in the Page. 


Some time back I one of my friend was developing an ERP kind of product where he was populating data based on the Database entries, like if type of the datasource is SP,Table,view or Web service; he was fething the data from the database(s) but calling the Web service he has to depent upon static proxy class created at the time of development.

During the customization of the product he can change only DB entries, aspx and javascript(s).

Using the code

I have created One Ajax web service project and one Web project in VS 2008.

The web service return the current server time and made it Ajax enabled by putting    [System.Web.Script.Services.ScriptService] attribute at class level.


I am calling this web service from different project, having one simple form.

On button click I am invoking web service by using Sys.Net.WebServiceProxy class provide by Ajax.

var webServicePath='http://localhost/TimeWebService/TimeService.asmx';
       var webMethod='GetServerTime';
           webMethod, false,{}, OnSucceeded,
           OnFailed,"User Context",1000000);
function OnSucceeded(result, eventArgs)
            var RsltElem = document.getElementById("lblTime");
            RsltElem.innerHTML = result;

I have developed the sample in C# and uploaded the sample in zip format.




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Aneesur Rehman Khan
Team Leader
India India
Anees is working as Sr. Team Lead based in Delhi(India).He is Post graduated in Computer applications and science.

He is having around 11 years of design,analysis and coding experience in Sharepoint, ASP.NET, C#, VB.NET, SQL Server 2000/05, Reporting Services,Analysis Services,VB 6.0 and Crystal Reports.

Comments and Discussions

GeneralThis is NOT a Javascript Proxy for WebServices Pin
Dewey7-Apr-09 20:06
memberDewey7-Apr-09 20:06 
GeneralRe: This is NOT a Javascript Proxy for WebServices Pin
Aneesur Rehman Khan7-Apr-09 20:29
memberAneesur Rehman Khan7-Apr-09 20:29 
General[Message Deleted] Pin
Moim Hossain6-Apr-09 8:02
memberMoim Hossain6-Apr-09 8:02 
GeneralRe: [My vote of 1] What's is so special about it? Pin
Member 30019106-Apr-09 9:08
memberMember 30019106-Apr-09 9:08 
GeneralRe: [My vote of 1] What's is so special about it? Pin
Aneesur Rehman Khan7-Apr-09 20:23
memberAneesur Rehman Khan7-Apr-09 20:23 
GeneralRe: [My vote of 1] What's is so special about it? Pin
Aneesur Rehman Khan7-Apr-09 20:28
memberAneesur Rehman Khan7-Apr-09 20:28 
RantRe: [My vote of 1] What's is so special about it? Pin
MikeStelmat8-Jun-09 14:27
memberMikeStelmat8-Jun-09 14:27 
GeneralRe: [My vote of 1] What's is so special about it? Pin
rilov30-Aug-09 11:12
memberrilov30-Aug-09 11:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 6 Apr 2009


13 bookmarked