Calling a method inside web service in action script 3.0

淺唱寂寞╮ 提交于 2019-12-03 09:14:52

Here is the code that I used in one of my Flex projects...

import mx.rpc.soap.WebService;

public var service:WebService = new WebService();

override protected function initializationComplete():void
{
    service.wsdl = "http://localhost:1133/YourService.asmx?wsdl"

    // GetPayload is the method name you're calling on your web service
    service.GetPayload.resultFormat = "e4x";
    service.GetPayload.addEventListener("result", yourResultHandler);
    service.GetPayload.addEventListener("fault", yourFaultHandler);

    // Method to call once the WSDL is loaded
    service.addventListener(LoadEvent.LOAD, loadHandler);

    service.loadWSDL();
}

Then here is what happens once the WSDL is loaded

protected function loadHandler(event:LoadEvent):void
{
    // send() takes the service parameters
    service.GetPayload.send("Product");
}

You just need to write the two methods to handle the XML returned by your services (the data is returned in e4x format:

protected function yourResultHandler(event:ResultEvent):void
{
    _messageXml = XML(event.result);
}

proteted function yourFaultHandler(event:FaultEvent):void
{
    Alert.show(event.toString());
}

I use something like this:

var request:URLRequest = new URLRequest();
request.url = 'http://example.org';

// If you're POSTing data:
request.method = URLRequestMethod.POST;
request.data = new URLVariables({ /* Your object */ });

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES; // If you're using POST
try {
    loader.load(request);
} catch(error:Error) {
    // Handle error
}

trace(loader.data); // Result

Documentation:

you can use the web services by one of the tricky method first you make swf by compiled in flex environment which includes the import statements of webservice like import mx.rpc.webservices. now compile it you will get a swf. now you go to as3.0 and make a empty movieclip on stage and in linkage property put it import for runtime sharing and put the a.swf(ex)on textbox in sharing.now you can import the statement in your action script file import mx.rpc.webservices.and use the method same as flex. definately u will be able to access web services....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!