Flex provides
This article uses WebService developed in C# language under the .NET platform as a remote data source to introduce in detail the data communication knowledge points between Flex and .NET's WebService; including connecting to WebService and remotely calling WebService methods , passing parameters and other related knowledge points to the WebService method. The usage of the three tags is basically the same. Here we take the
First take a look at the following code block:
1
3useProxy=" false">
4
5
6
wsdl attribute can be specified to the wsdl address of the WebService to be accessed, which defines two operation tags (
1///
2///return string
3///< /summary>
4///
5[WebMethod]
6publicstringHelloWorld()
7{
8return"HelloWorld";
9}
10
11///
12///Return a simple object
13///
14///
15[WebMethod]
16publicBookGetBook()
17{
18returnnewBook
19{
20Id=1,
21Name="Romance of the Three Kingdoms",
22Author="Luo Guanzhong",
23Price=100
24};
25}
The above is the WebService method definition and passed in the Flex client (mxml)< mx:WebService> tag to access the complete process of WebService. Let’s take a look at how to call the methods defined by WebService on the Flex client:
1
2< ![CDATA[
3importmx.controls.Alert;
4importmx.rpc.events.FaultEvent;
5importmx.rpc.events.ResultEvent;
6
7/**
8* Initiate a request to WebService - call the HelloWorld method, dataService is the id of
9**/
10internalfunctiononRequest():void
11{
12dataService.HelloWorld();
13}
14
15/**
16*Request result returned after successful processing
17**/
18internalfunctiononSuccess(evt:ResultEvent):void
19{
20Alert.show(evt.result.toString());
21 }
22
23
24/**
25*Handling function for request failure
26**/
27internalfunctiononFault(evt:FaultEvent):void
28{
29Alert.show("Failed to access WebService!");
30}
31]]>
32
Through the above call, you can complete an interaction between Flex and .NET WebService. Of course, we can also pass parameters when calling WebService on the Flash/Flex client. The following WebMethod definition of WebService is as follows:
1///
2///will be passed in Convert the parameters to uppercase characters and return
3///
4///
5///
6[WebMethod]
7publicstringConvertToUpper(stringvalue)
8{
9returnvalue.ToUpper();
10}
You can access it by configuring
1
1 /* *
2*Initiate a request to WebService
3**/
4internalfunctiononRequest():void
5{
6//dataService.HelloWorld();
7dataService.ConvertToUpper("abcdefg");
8}
In addition, we can also pass
Go back to the front and take a look at the method definition of WebService. One of the methods, GetBook, returns a Book object. If it is the returned object, how do we get the value of this object on the Flex client? For details, see the following code example:
1internalfunctiononObject():void
2{
3dataService.GetBook();
4}
5
6internalfunctiononObjectSuccess(evt:ResultEvent):void
7{
8 //Get the return value directly through the event's result attribute, and then access the attribute directly to OK
9Alert.show(evt.result.Name);
10}
11
12/**
13*Handling function for request failure
14**/
15internalfunctiononFault(evt: FaultEvent):void
16{
17Alert.show("Access to WebService failed!");
18}