flex 访问webservice方法及跨域问题解决
一、flex调用webserivice代码
import mx.rpc.soap.WebService;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var webService:WebService; protected function getWebService():void { var ws:WebService = new WebService(); //ws.wsdl= "http://localhost:9999/wsServerExample?wsdl"//java ws.wsdl = "http://192.168.179.14:8082/WebServiceTest/WebServiceHelloPort?wsdl";//java //ws.wsdl = "http://192.168.179.254/ConvertFeaturesToJson/ConvertService.asmx?wsdl";//.net //ws.useProxy = true; ws.addEventListener(ResultEvent.RESULT,resultFunction);//增加返回结果的监听 ws.addEventListener(FaultEvent.FAULT,onFault); //ws.getOperation("getGreeting").send("ok"); ws.getOperation("SayHello").send("from sun");//SayHello为调用的方法名,from sun为传递进去的字字符串参数 //ws.getOperation("Hello").send("ok"); ws.loadWSDL(); } private function onFault(e:FaultEvent):void { trace(e.message); //给用警告,偷懒不愿debug的时候这么来 Alert.show("未能连接到WebService,请检查网络!","错误"); } private function resultFunction(event:ResultEvent):void{ var s:String = event.result as String; Alert.show(s,"返回结果"); }
二、策略文件(crossdomain.xml)内容
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd" > <cross-domain-policy> <site-control permitted-cross-domain-policies="all" /> <allow-access-from domain="*" /> <allow-http-request-headers-from domain="*" headers="*"/> </cross-domain-policy>
记住对于Flash Player 9而言,crossdomain.xml文件内容出现了较大的变化<allow-http-request-headers-from domain="*" headers="*"/> 这个tag不可以少,否则访问webservice会报错。
tomcat中将crossdomain.xml文件放到ROOT根目录下。
iis中将crossdomain.xml文件放到wwwroot根目录下。
多看一行书,就少写一行代码,记录点滴,用心生活。