Atlas well encapsulates Web service calls, making it very simple to call web service using Js. You only need to use webservicename. webmethod () to complete the call. This article uses two simple examples to illustrate this content.
Main Content
1. Call a simple Web Service
2. transfer complex types of data
Atlas well encapsulates Web service calls, making it very simple to call web service using Js. You only need to use webservicename. webmethod () to complete the call. This article uses two simple examples to illustrate this content.
1. Call a simple Web Service
In this example, we call web service to return a string. First, we create a simple web service and compile a web method that accepts string parameters.
[Webmethod]
Public String echostring (string S)
{
Return "Hello:" + S;
}
Create a Web page, add scriptmanager to the page, and introduce the required web service in the servicereference subcontrol.
<Atlas: scriptmanager id = "scriptmanager" runat = "server" enablescriptcomponents = "true">
<Services>
<Atlas: servicereference Path = "simplewebservice. asmx"/>
</Services>
</Atlas: scriptmanager>
Now we can call Web Service in JS. Note that the echostring method has only one parameter. Here we pass two parameters. The first one is obviously the parameter that the echostring method should have, the second oncomplete calls the callback method when the method is successfully returned:
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function onbuttongo_click ()
{
// Call script proxy passing the input element data
Requestsimpleservice = simplewebservice. echostring (
Document. getelementbyid ('inputname'). Value, // Params
Oncomplete // Complete Event
);
Return false;
}
Function oncomplete (result)
{
Alert (result );
}
</SCRIPT>
After compilation and running:
Call:
2. transfer complex types of data
In the above example, we just made a simple example of calling Web Service, and the type we encounter in actual applications will be more complex. Let's look at another example, it will return a custom type. First, it defines a pure data class animal without any operation:
Public class animal
{
String _ name;
String _ color;
Public string name
{
Get {return _ name ;}
Set {_ name = value ;}
}
Public String color
{
Get {return _ color ;}
Set {_ color = value ;}
}
}
Write a web service and directly return the result after receiving the complex type:
[Webmethod]
Public animal echoanimal (animal)
{
Return;
}
Create a Web page, add scriptmanager to the page, and introduce the required web service in the servicereference subcontrol.
<Atlas: scriptmanager runat = "server" id = "scriptmanager">
<Services>
<Atlas: servicereference Path = "complexwebservice. asmx"/>
</Services>
</Atlas: scriptmanager>
Provided to the user input interface:
<H3>
Name: <input id = "inputname"/>
Color: <input id = "inputcolor"/>
<Input id = "buttongo" type = "button" value = "go" onclick = "Return onbuttongo_click ()"/>
</H3>
Now you can add the corresponding JS and return the returned result alert:
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function onbuttongo_click ()
{
// Call script proxy passing the input element data
VaR I1 = Document. getelementbyid ('inputname ');
VaR I2 = Document. getelementbyid ('inputcolor ');
VaR object = new animal ();
Object. Name = i1.value;
Object. Color = i2.value;
Requestcomplexservice = complexwebservice. echoanimal (
Object, // Params
Oncomplete // complete eventt
);
Return false;
}
Function oncomplete (result)
{
Alert ("name =" + result. Name + "color =" + result. Color );
}
</SCRIPT>
After compilation and running:
Call:
It can be seen that calling a Local Web Service in Atlas is very simple, and there are some differences between calling a remote web service. We will discuss later that in actual use, we also need to consider handling errors and timeouts [Examples in this article are from the official Atlas website].