![]() |
Web Development »
Web Services »
Beginners
Beginner
License: The Code Project Open License (CPOL)
Invoking WebServices using JavaScriptBy Duaa Al-ansariIn this tutorial I am going to show how to invoke web services, how to write to file and how to run file using JavaScript. |
Javascript, Windows, ASP.NET, IIS, Visual Studio
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Done by: Duaa Al-ansari - Jordan.
JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Mozilla, Firefox, Netscape, and Opera.
JavaScript is used in millions of Web pages to add functionality, validate forms, detect browsers, and much more.
And with the age of Web Services, come the need to call web services using JavaScript.
In this tutorial I’m going to explain the following:
· How to use JavaScript to invoke web services APIs.
· How to use JavaScript to write to a file.
· How to use JavaScript to run application.
I will write the result ICA file content to a file using JavaScript and then run that file using JavaScript also.
(For more information about Extentrix web services 2.0 – Application Edition visit http://www.extentrix.com/Web%20Services/Index.htm ).
Enable Initialize and script ActiveX controls option. For Internet Explorer, do the following:
A) From Internet Explorer, go to Tools >> Internet Options.
Figure 1
B) Select Security tab, go to Custom level.
Figure 2
C) Enable "Initialize and script ActiveX controls not marked as safe for scripting" option and click OK.
Figure 3
The WebService behavior is a simple, lightweight component that encapsulates the capability to invoke remote methods using the Simple Object Access Protocol (SOAP). This behavior enables Microsoft Internet Explorer 5 and later to communicate directly with Microsoft .NET WebServices and other platforms, applications, and servers that support Web Service Description Language (WSDL) 1.1.
Using the WebService behavior, browser scripts can access data from Web Services directly and use the information to update Web pages dynamically with DHTML. This new capability significantly improves the browsing experience and is faster and more efficient than the traditional approach, which requires a full refresh of the Web page.
The WebService behavior supports a wide variety of data types including: intrinsic SOAP data types, arrays, objects, arrays of objects and Extensible Markup Language (XML) data.
1. JavaScript_Sample.htm:
This page is the main page in this sample; it contains a link to lunch Acrobat Reader Application. It connects to Extentrix web services using JavaScript to lunch the Acrobat Reader published application on Citrix presentation server using ICA client.
2. webservice.htc:
It is an HTML Components (HTC) file that encapsulates the web service behavior. It should be enabled in order to be able to connect with the web service. You can download it from this link: http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/samples/internet/behaviors/library/webservice/default.asp.
// Create hyper link html object
<a id="ViewLink" href="http://" onclick="CallAPI()" style="behavior:url(webservice.htc);"
onresult="onmyresult();"> <font color=maroon>Acrobat Reader</font></a>
Then I set the web service description language (WSDL) URL for the web service behavior attached to “ViewLink” hyper link. And set a friendly name “Service1" for a Web service using the useService method.
useService method enables us to establish a friendly name for a Web Service URL connection that can be referenced from script instead of the long URL.
And in this method we define the web service URL that will be contact when the hyper link "ViewLink" event occurs.
I put the calling to the useService method inside init() method which will be called at the load time (in the onload event handler), you can place it in the onload event handler directly. The main idea is to be sure that this method is called before the web service behavior event attached to the hyper link “ViewLink” occurs.
For more information about useService method http://msdn2.microsoft.com/en-us/library/ms531063(VS.85).aspx
// creating init() function that calls the webservice:
function init()
{
service = document.getElementById("ViewLink");
service.useService("http://www.extentrix.com/webservices/2.0.0/ExtentrixWebServicesForCPS.
asmx?WSDL","Service1");
}
// where service is the enabling link in the previous step.
// calling init() function at page load:
<BODY onload=init();>
// Define my credentials class:
function Credentials(UserName,Password,Domain,Type,Method)
{
this.UserName=UserName;
this.Password=Password;
this.Domain=Domain;
this.DomainType=Type;
this.PasswordEncryptionMethod=Method;
}
The callService() method initiates the engagement of the WebService behavior with the Web service. Here is its syntax:
iCallID = sElementID.sFriendlyName.callService( [oCallHandler], funcOrObj, oParam);
Where:
· iCallID is the returned ID of the service call. In case of asynchronous call, this ID should be matched with the ID returned as a property of the result object. Only when matched can the result object be associated with this service call.
· sFriendlyName is the friendly name associated with the Web service. Call the useService() method to assign a friendly name to your Web service.
· oCallHandler is the callback handler function for processing the result object. This parameter is optional.
· funcOrObj is one of the following possible values.
strFuncName: A string representing the name of the remote function being called. It must be bounded by single or double quotation marks.
objCall: A call object, which has the necessary properties defined to call a remote function. This parameter is required.
· oParam are a comma-delimited list of parameters that the Web service's API expects. This parameter is required.
// Calling the API:
function CallAPI()
{
var myusername = "citrixdesktop";
var mypassword = "demo";
var mydomain = "testdrive";
var mytype = 0;
var mymethod = 0;
var credentials = new Credentials(myusername,mypassword,mydomain,mytype,mymethod);
try
{
iCallID = service.Service1.callService("LaunchApplication", "PDFViewer",
credentials ,"w2k3-s3","172.19.8.103");
}
catch(e)
{
alert(e.message);
}
}
The onresult event fires when a result has been received from a remote Web service using the WebService behavior. It is only available to objects in the document to which the WebService behavior is attached.
The event object for onresult event is generally available in the Result object. This contains the following properties:
· result.id: Returns a unique identifier that is associated with a particular instance of the callService() method call.
· result.value: Returns the value, or values, of the method call. The data type returned depends on the definition of the method in the service description.
· result.raw: Returns the whole XML data received from the server, including the packet headers and envelopes when SOAP is used.
· result.error: Returns a Boolean, specifying if there has been an error. If true, the method call resulted in an error; if false, the method was called successfully.
When result.error is true and iCallID equals event.result.id, an error occurred. Hence the errorDetail object will be available as a property of the result object:
· result.errorDetail.code :A cryptic error code. Can be VersionMismatch, MustUnderstand, Client, or Server.
· result.errorDetail.string: A more descriptive error message. For example: "Error is Invalid argument."
· result.errorDetail.raw: The entire XML data packet, received from the server.
// Catch the result of calling the API:
function onmyresult()
{
if ((event.result.error)&&(iCallID==event.result.id))
{
var xfaultcode = event.result.errorDetail.code;
var xfaultstring = event.result.errorDetail.string;
var xfaultsoap = event.result.errorDetail.raw;
alert("Error");
}
Else
{
var icaContent = event.result.value;
array = icaContent.split('\n');
var fileName = "c:/content.ica";
var Fs = new ActiveXObject("Scripting.FileSystemObject");
var Output = Fs.OpenTextFile(fileName,2,true);
var content = array[0] + "\n";
for (i = 1; i < array.length; i++)
{
content += array[i] + "\n";
}
Output.write(content);
Output.close();
Set the file URL.
Create an ActiveXObject assigned to the WScript.Shell object.
Use run function that take file url as a parameter.
url = "file:///"+ fileName;
WSH = new ActiveXObject("WScript.Shell");
WSH.run(url);
}
}
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 12 Feb 2008 Editor: |
Copyright 2008 by Duaa Al-ansari Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |