Javascript|web
Using JavaScript to invoke WebService under. NET, you use WebService behavior. Here is an example to explain, relatively simple
1, first of all, to create a webservice, such as
<%@ WebService language= "C #" Class=mymath%>
Using System;
Using System.Web.Services;
public class MyMath {
[WebMethod]
public int Add (int a, int b)
{
return a + B;
}
[WebMethod]
public int subtract (int a, int b)
{
return a-b;
}
}
Then publish it and get its WSDL first.
2, first of all, we want to download WEBBEHAVIOR.HTC this file (can go to http://msdn.microsoft.com/downloads/samples/internet/behaviors/library/ Webservice/default.asp.)
Go to the download, then put it in your web's current directory and then in the page where you want to invoke Webserice, modify the following
<body>
<div id= "AddService" style= "Behavior:url (WEBSERVICE.HTC)" ></div>
</body>
Here we name the Div ID as a meaningful name and specify style as the WebService behavior. Next, we'll write JavaScript to invoke Webserice:
First, in JavaScript, we call its Wsdladdservice.useservice ("Http://localhost/services/math.asmx?"). WSDL "," MyMath "); use Id.useservice (wsdll path, simple naming method);
We have previously set the ID is AddService, and in order to call the client convenience, we have a name here, called MyMath. And in order to ensure the correct call Webserice, must be in the body of the OnLoad event, immediately loaded processing WebService call JavaScript, as follows
<script language= "JavaScript" >
function init ()
{
Addservice.useservice ("Http://localhost/services/math.asmx?WSDL", "MyMath"); }
</script>
<body >
<div id= "service" style= "Behavior:url (WEBSERVICE.HTC)" >
</div>
</body>
On the above, we get the WSDL to return webservice first by WebService behavior, and then we call it, and the format of the call is as follows: Icallid = ID. Friendlyname.callservice ([CallbackHandler,] "methodname", Param1, Param2, ...);
Here ID is the ID that we set in Div, and Fridndbyname is the life that we have just for aspect, here is MyMath, and CallbackHandler is the procedure name that uses callback function, if have no setting, The default is to use the method invoked by Onresult, which is described below, and param1,,param2 is the argument passed in, such as:
<script language= "JavaScript" >
All of these variables must is global,
Because they are used in both init () and Onresult ().
var icallid = 0;
var IntA = 5;
var IntB = 6;
function init ()
{
Establish the friendly name "MyMath" for the WebServiceURL
Service.useservice ("/services/math.asmx?") WSDL "," MyMath ");
The following method doesn ' t specify a callback handler. So Onwsresult () is used
Icallid = service. Mymath.callservice ("Add", IntA, IntB);
}
function Onwsresult ()
{
If there is a error, and the call came from the call () in Init ()
if ((Event.result.error) && (icallid==event.result.id)
{
Pull the error information from the Event.result.errorDetail properties
var xfaultcode = Event.result.errorDetail.code;
var xfaultstring = event.result.errorDetail.string;
var xfaultsoap = Event.result.errorDetail.raw;
ADD code to handle specific error codes
}
If there is no error, and the call came from the call () in Init ()
else if ((!event.result.error) && (icallid = = event.result.id))
{
Show the arithmetic!
Alert (IntA + ' + ' + intB + ' = ' + Event.result.value);
}
Else
{
Alert ("Something else fired the event!");
}
}
</SCRIPT>
<body >
<div id= "service" style= "Behavior:url (WEBSERVICE.HTC)" onresult= "Onwsresult ()" >
</div>
</body>
Note, in the Onresult way to return, in the div section of the Onresult to specify the method of processing, here is the Onwsresult () method, which based on the information returned to determine whether the error, the error is displayed.
If you use a callback, the following is handled
<script language= "JavaScript" >
All of these variables must is global,
Because they are used in both init () and Onresult ().
var icallid = 0;
var IntA = 5;
var IntB = 6;
function init ()
{
Establish the friendly name "MyMath" for the WebServiceURL
Service.useservice ("/services/math.asmx?") WSDL "," MyMath ");
The following uses a callback handler named "Mathresults"
Icallid = service. Mymath.callservice (Mathresults, "Add", IntA, IntB);
}
function Mathresults (Result)
{
If there is a 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;
ADD code to handle specific error codes
}
If there was no error
Else
{
Show the Arithmetic
Alert (IntA + ' + ' + intB + "=" + Result.value);
}
}
</SCRIPT>
<body >
<div id= "service" style= "Behavior:url (WEBSERVICE.HTC)" >
</div>
</body>