Click here to Skip to main content
15,896,359 members
Articles / Web Development / HTML

Remote Scripting - Calling a WebService with JavaScript and C#

Rate me:
Please Sign up or sign in to vote.
3.85/5 (11 votes)
18 Feb 20052 min read 293.9K   5K   46  
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.
<%@ Page language="c#" Codebehind="MDemo.aspx.cs" AutoEventWireup="false" Inherits="RemoteScriptingDemo1.MDemo" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>MDemo</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
		<SCRIPT language="JavaScript">
var iCallID;
function init()
{
	
    service.useService("proxy.asmx?WSDL","proxy");
	Form1.btnSuma.disabled = true;
	service.onserviceavailable = enableButton();
}
function enableButton(){
Form1.btnSuma.disabled = false;
}


function doSuma(y, x){
    oSPAN.innerText = y + " + " + x + " = ";
    // The following example uses a callback handler named "mathResults"
    iCallID = service.proxy.callService(myResults,  "Adicion", y,x);
}


function myResults(result){
    // If there is an error, and the call came from the call() in init()
    if(result.error){
    // Pull the error information from the event.result.errorDetail properties
        var xfaultcode   = result.errorDetail.code;
        var xfaultstring = result.errorDetail.string;
        var xfaultsoap   = result.errorDetail.raw;
		oSPAN.innerText = xfaultcode + " " + xfaultstring + " " + xfaultsoap;
    }// If there was no error
    else{
        // Show the arithmetic
          oSPAN.innerText += result.value;     
    }
}
		</SCRIPT>
	</HEAD>
	<body onload="init()">
		<form id="Form1" method="post" runat="server">
			<div id="service" style="BEHAVIOR: url(webservice.htc)"></div>
			<P><BR>
				<BR>
				M-Ecuacion&nbsp;: <SPAN id="oSPAN"></SPAN>
				<BR>
				<BR>
				<INPUT id="texta" type="text"><INPUT id="textb" type="text"><BUTTON id="btnSuma" onclick="doSuma(texta.value,textb.value);" type="button">Suma 
					Client</BUTTON>
			</P>
			<P>&nbsp;</P>
		</form>
	</body>
</HTML>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Founder Cimar Solutions
Mexico Mexico
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

Comments and Discussions