Use javascript to call webservice details in. net

Source: Internet
Author: User

Webservice behavior is used when webservice is called using javascript in. net. The following is an example, which is relatively simple.

1. First, create a webservice, such

<% @ 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;
}
}
Release the SDK and obtain its wsdl.
2. First, we want to download the webbehavior. htc file (you can go to http://msdn.microsoft.com/downloads/samples/internet/behaviors/library/webservice/default.asp .)
Download the file and put it in your current web directory.
Then, on the page where you want to call 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 the style as webservice behavior. Next, we need to write javascript to call webserice:
First, we call its wsdladdservice in javascript.UseService("Http: // localhost/services/math. asmx? WSDL "," MyMath ");
Use id.UseService(WSDLL path, simple naming method );
The id we set previously is addservice. To make it easier for the client to call, we have set the name MyMath. To ensure correct webserice calling, javascript for processing webservice calls must be loaded immediately in the onload event in the body, as shown below:
<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 section, we first get the wsdl of the webservice returned through the webservice behavior, and then we will call it. The call format is as follows: iCallID = id. FriendlyName.CallService([CallbackHandler,] "MethodName", Param1, Param2 ,...);
Here, id is the id we set in div, while FridndbyName is the name we just set for the aspect. Here is MyMath, while CallbackHandler is the process name using the callback function, if no parameter is set, the onresult method is used for processing by default. As described below, param1, param2, and so on refer to the input parameters, for example:
<SCRIPT language = "JavaScript">
// All these variables must be global,
// Because they are used in both init () and onresult ().
Var iCallID =;
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 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: If onresult is returned, you must specify the processing method in the onresult of the div part. Here, the onWsresult () method is used to determine whether an error is returned Based on the returned information, if an error occurs, it is displayed.
If callback is used, the process is as follows:
<SCRIPT language = "JavaScript">
// All these variables must be global,
// Because they are used in both init () and onResult ().
Var iCallID =;
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 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.