Click here to Skip to main content
Click here to Skip to main content

Remote scripting - Calling a WebService with JavaScript and C#

By , 18 Feb 2005
 

Introduction

I have been using webservices for the past two years, but whenever I use a function, I have to post back all my pages. Using remote scripting we can call our webservices from the client, using JavaScript, and there is no need to post back all the page content, just an instruction. We send the line, catch the result and show it. We need not do a post back, the operation is processed behind, blind to the user, and fast as a "line go-line comes".

<script language="JavaScript">
init(){
 service.useService("http://localhost/CursoAspNetWebService/Service1.asmx?WSDL",_
                                                                     "Service1"); 
}
function tst(){ 
   iCallID = service.Service1.callService("Suma",ip1.value,ip2.value); 
}
function onmyresult(){ 
   service.innerHTML= "Resultado : " + event.result.value; 
} 
</script>
<body onLoad="init();">
  <button onclick="javascript:tst()" ID="Button1">Call Add Web Method</button>
  <div id="service" style="BEHAVIOR:url(webservice.htc)" onresult="onmyresult();">
  </div>
</body>

The source code includes the webservice.htc, the HTML component that is used.

The HTML

We need to create an interface using just HTML controls, because we don't want to send the page to the server. We need just a text, and a button with a function:

onclick="doSuma(texta.value,textb.value)

and the section where we want to display the result, like a <Div>.

id="service" style="BEHAVIOR: url(webservice.htc)"

The Init script

We need to put init() in the load of HTML, and the function doSuma(...) is called by clicking the button:

function init() { 
    service.useService("proxy.asmx?WSDL","proxy"); 
}
function doSuma(y, x){ 
    oSPAN.innerText = y + " + " + x + " = "; 
    iCallID = service.proxy.callService(myResults, "Adicion", y,x); 
}

The iCallID is a var that is unused later, but the result comes with the call from it. We need to fill parameters to:

  1. Call a function to manage the result.
  2. Call the public function.
  3. Send the values to process, from the 3rd to the necessary values needed.

The MyResult JavaScript function

function myResults(result){ 
    if(result.error){ 
        var xfaultcode = result.errorDetail.code; 
        var xfaultstring = result.errorDetail.string; 
        var xfaultsoap = result.errorDetail.raw; 
        oSPAN.innerText = xfaultcode + " " + xfaultstring + " " + xfaultsoap;
    }
    else{ 
        oSPAN.innerText += result.value; 
    } 
} //the end of function

The local proxy

The local proxy has the goal of giving security to the application. We have seen in JavaScript that a URI points to localhost. If we don't use the proxy in localhost we need to give the full URI of the webservice, and we give other users the door wide open. In this case, we can apply security by forms level protection in ASP.NET

The proxy just has an instance of the real web service and a single call of a function:

using System;
using System.Web;
using System.Web.Services;

namespace RemoteScriptingDemo1
{
 public class proxy : System.Web.Services.WebService
 {
  public proxy()
  {
   InitializeComponent();
  }

  private IContainer components = null;
    
  private void InitializeComponent()
  {
  }

  protected override void Dispose( bool disposing )
  {
   if(disposing && components != null)
   {
    components.Dispose();
   }
   base.Dispose(disposing);  
  }
  

  [WebMethod]
  public decimal Adicion(decimal x, decimal y)
  {
   WSDemo.Service1 ws = new RemoteScriptingDemo1.WSDemo.Service1();
   return ws.suma(x,y);
  }
 }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Marcelo Lujan [El Bebe.Net ]
Founder Cimar Solutions
Mexico Mexico
Member
Ing. F. Marcelo Lujan alias El Bebe Dot Net. Hola, yo desarrollo de forma independiente en C#. ASP.NET y Win32 Diseño Macromedia etc. con mas de 10 años de experiencia en informática y soporte a sistemas, así como desarrollo de software y nuevos productos.
 
Espero que ayude la informacion que pongo a su disposicion.
I Hope this information that i upload to codeproject helps you.
Atte: Marcelo Lujan

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberAli Al Omairi(Abu AlHassan)23 Mar '11 - 4:05 
100 Rose | [Rose]
GeneralVery Thanks!!!!memberdiargo6 Oct '09 - 12:14 
Its Great!!!!
QuestionAdd WebService Behaviour dynamically.membermadhu cellulo3 Sep '08 - 21:24 
HI,
WebService behaviour is attached to HTML Element at design time.I want to add that behaviour dynamically.I've tried with 'addBehaviour('..htc')' method.But its not working.Is there any work around?
QuestionHow about AJAXmembersemenoff26 Oct '07 - 2:17 
The same problem can easily be solved by using ASP.Net AJAX. You can find my article describing this approach in the ASP.Net AJAX of my website.
Questionhow we call asp.net function/procedure in Javascript.membersariya_jesus@yahoo.co.in19 Jul '06 - 16:48 
how we call asp.net function/procedure in Javascript.plz help me urgent
AnswerRe: how we call asp.net function/procedure in Javascript.memberMarcelo Lujan [El Bebe.Net ]20 Jul '06 - 6:37 
if the function returns XML U can use this sample to get the result.
 
:Marcelo [El Bebe.net] Lujan
_____________________
Ing. Marcelo Lujan
Cimar
Monterrey, Mexico
NewsA cross browser SOAP clientmemberMatthias Hertel18 Jul '06 - 5:48 
A cross browser implementation for JavaScript that can call a Webservice is also available here on codeproject at http://www.codeproject.com/soap/JavaScriptProxy_01.asp.
GeneralNot worked in operamemberurd_sheir21 Jun '06 - 2:41 
this code is not working on opera. how i will make it work on opera. what changes should be made. please tell me. thanx
 
ayaz
GeneralRe: Not worked in operamemberavavital6 May '09 - 3:44 
u r write htc is Microsoft only
GeneralAbout soap call using C#membersumeet.polaris4 Jun '06 - 18:11 
Hi,
 
Can you plz guide me as to How can I make a soap call using C#.
 
Request immdediate reply at sumeet_koshal@ml.com.
 

Thanks
Sumeet
Generaloriginal article at microsoftmemberBorecast30 May '06 - 5:21 
Microsoft provides this information in more detail:
http://msdn.microsoft.com/workshop/author/webservice/using.asp
GeneralRe: original article at microsoftmemberMarcelo Lujan [El Bebe.Net ]30 May '06 - 12:48 
Both are originals.
 
This article was Published on february 2005.
 
:Marcelo [El Bebe.net] Lujan
_____________________
Ing. Marcelo Lujan
Cimar
Monterrey, Mexico
Questioncan u change URLmembersugandh28 Mar '05 - 8:11 
hi,
can u change the url like google does it in googlenews?
AnswerRe: can u change URLmemberAhmed Galal5 Mar '06 - 5:43 
i need to change the url without moving the page,
where did u find it on google news ?
GeneralThis is how Office Web Component works :)sussPhilippe Graça22 Feb '05 - 20:07 
I'm familiar with this techno for one year now thanks to MS great article:
http://msdn.microsoft.com/msdnmag/issues/03/10/OLAP/default.aspx
There is great functionalities you can implement. The one I use is to feed a tree view on the fly without doing posts to the server, which is great.
Regards
Philippe Graça

GeneralThis is a cool startmemberleonleslie19 Feb '05 - 11:46 
But what about more complicated calls that may return rows of data from a database.....
 
Programming in your interest
GeneralRe: This is a cool startsussMarcelo Lujan [El Bebe.Net ]21 Feb '05 - 6:46 
Hi
I have just upload this other article.
view the next link:
http://www.codeproject.com/useritems/marcelo888RemoteScript02.asp
 
I hope this will help's you.
Cool | :cool:
 
:Marcelo [El Bebe.net] Lujan
_____________________
Ing. Marcelo Lujan
Cimar
Monterrey, Mexico

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 18 Feb 2005
Article Copyright 2005 by Marcelo Lujan [El Bebe.Net ]
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid