Click here to Skip to main content
15,881,839 members
Articles / Web Development / XHTML

Invoke ASP.NET Web service from javascript without proxy

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
8 Apr 2009CPOL2 min read 43.6K   461   13   8
This sample demonstrates how to call an external Ajax web service without creating code behind proxy from JavaScript

Introduction 

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. 

Background

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';
       Sys.Net.WebServiceProxy.invoke(webServicePath,
           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.
 

History

 

License

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


Written By
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
Dewey7-Apr-09 20:06 
GeneralRe: This is NOT a Javascript Proxy for WebServices Pin
Aneesur Rehman Khan7-Apr-09 20:29
Aneesur Rehman Khan7-Apr-09 20:29 
General[Message Deleted] Pin
Moim Hossain6-Apr-09 8:02
Moim Hossain6-Apr-09 8:02 
GeneralRe: [My vote of 1] What's is so special about it? Pin
Member 30019106-Apr-09 9:08
Member 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
Aneesur 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
Aneesur Rehman Khan7-Apr-09 20:28 
RantRe: [My vote of 1] What's is so special about it? Pin
MikeStelmat8-Jun-09 14:27
MikeStelmat8-Jun-09 14:27 
GeneralRe: [My vote of 1] What's is so special about it? Pin
rilov30-Aug-09 11:12
rilov30-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.