If you use javascript to call webservice under .net, you need to use webservice behavior. The following is an example to explain, it is relatively simple
1. First, 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, we need to download the file webbehavior.htc (you can go to http://msdn.microsoft.com/downloads/samples/internet/behaviors/library/webservice/default.asp .)
Download it and put it in the current directory of your web. Then in the page where you want to call webserice, modify it as follows
<body>
<div id="addservice" style="behavior:url(webservice.htc)"></div>
</body>
Here we name the div id something meaningful and specify the style as webservice behavior. Next, we need to write javascript to call webserice:
First, we call its wsdladdservice.useService(" http://localhost/services/math.asmx?WSDL", "MyMath "); in javascript.
Use id.useService(WSDLL path, simple naming method);
The id we set before was addservice, and in order to make it easier for the client to call it, we named it MyMath. In order to ensure that webserice can be called correctly, the javascript that handles webservice calls must be loaded immediately in the onload event in the body, as follows
<script language="JavaScript">
function init()
{
addservice.useService(" http://localhost/services/math.asmx?WSDL","MyMath "); }
</script>
<body onload="init()">
<div id="service" style="behavior:url(webservice.htc)">
</div>
</body>
In the above, through the webservice behavior, we first get the wsdl that returns the webservice. Next, we have to make a call. The call format is as follows: iCallID = id.FriendlyName.callService([CallbackHandler,] "MethodName", Param1, Param2, . ..);
Here id is the id we set in the div, and FridndbyName is the name we just named for the aspect, here is MyMath, and CallbackHandler is the process name of the callback function. If there is no setting, the default is to use onresult to call it. The method is used for processing, which will be discussed below, and param1, param2, etc. refer to the incoming parameters, such as:
<SCRIPT language="JavaScript">
// All these variables must be global,
// because they are used in both init() and onresult().
var iCallID = 0;
varintA = 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 an 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 here
}
// if there was 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 onload="init()">
<div id="service" style="behavior:url(webservice.htc)" onresult="onWSresult()">
</div>
</body>
Note that if you use the onresult method to return, you must specify the processing method in the onresult of the div part. Here, the onWsresult() method is used, in which the returned information is used to determine whether there is an error. If an error occurs, it is displayed.
If callbacks are used, proceed as follows
<SCRIPT language="JavaScript">
// All these variables must be global,
// because they are used in both init() and onResult().
var iCallID = 0;
varintA = 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 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;
// Add code to handle specific error codes here
}
// if there was no error
else
{
// Show the arithmetic
alert(intA + ' + ' + intB + " = " + result.value);
}
}
</SCRIPT>
<body onload="init()">
<div id="service" style="behavior:url(webservice.htc)">
</div>
</body>