![]() |
Languages »
XML »
Web Services
Intermediate
Remote scripting - Calling a WebService with JavaScript and C#By Marcelo Lujan [El Bebe.Net ]Calling a WebService with JavaScript. Security included by using a local asmx with the function of proxy, and not revealing the webservice URI in the JavaScript. |
C#, Javascript, XML, Windows, .NET1.1, ASP.NET, VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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)"
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:
public 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 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);
}
}
}

General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Feb 2005 Editor: Rinish Biju |
Copyright 2005 by Marcelo Lujan [El Bebe.Net ] Everything else Copyright © CodeProject, 1999-2010 Web19 | Advertise on the Code Project |