问题解决了,通过在<mx:WebService id="ws" wsdl="http://www.webservicex.net/TranslateService.asmx?wsdl" result="resultHandler(event)" showBusyCursor="true" useProxy="false">里添加一个fault="Alert.show(event.fault.faultString,'Error')",
也就是<mx:WebService id="ws" wsdl="http://www.webservicex.net/TranslateService.asmx?wsdl" result="resultHandler(event)" showBusyCursor="true" fault="Alert.show(event.fault.faultString,'Error')" useProxy="false">,当点击“开始翻译”按钮的时候,就提示“Security error accessing url”,由此可以判断出是跨域访问失败。
因此我找到WEB-INF/flex/proxy-config.xml文件,打开这个文件,添加如下内容
<destination id="mywebservice">
     <properties>
     <dynamic-url>
     http://www.webservicex.net/*
     </dynamic-url>
     </properties>
</destination>添加这段代码的目的就是让flash能访问http://www.webservicex.net/下的所有文件,这里的id我设置为mywebservice,那么在
<mx:WebService id="ws" wsdl="http://www.webservicex.net/TranslateService.asmx?wsdl" result="resultHandler(event)" showBusyCursor="true" fault="Alert.show(event.fault.faultString,'Error')" useProxy="false">里应该添加一个属性, destination="mywebservice",
因此mx:WebService的完整代码应为<mx:WebService id="ws" wsdl="http://www.webservicex.net/TranslateService.asmx?wsdl" result="resultHandler(event)" showBusyCursor="true" fault="Alert.show(event.fault.faultString,'Error')" useProxy="false" destination="mywebservice">到此为止,所有的工作就做完了,然后运行页面,就可以正常访问了,不过这个在线翻译的webservice本身有问题,现在好像不提供这项功能了,因此会提示类似于“无法完成翻译,请和XXXXX邮箱联系”。
没关系,我在网上找了一个天气预报的webservice,经过测试是可以用的,具体的代码我贴出来吧,供大家学习。
页面代码如下:
<?xml version="1.0" encoding="gb2312"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
internal function initApp():void{
var arr:Array = new Array();
arr.push({label:"北京",data:"北京"});
arr.push({label:"上海",data:"上海"});
lang_select.dataProvider = arr;
}

internal function doSearch():void{
ws.getWeatherbyCityName.send();
}

internal function resultHandler(evt:ResultEvent):void{
output_text.text = evt.result.toString();
}
]]>
</mx:Script>
<mx:WebService id="ws" wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl" fault="Alert.show(event.fault.faultString,'Error')" showBusyCursor="true" useProxy="false" destination="mywebservice">
<mx:operation name="getWeatherbyCityName" result="resultHandler(event)">
<mx:request>
<theCityName>{lang_select.selectedItem.data}</theCityName>
</mx:request>
</mx:operation>
</mx:WebService>
<mx:Panel x="57" y="10" width="481" height="380" layout="absolute" title="利用WebService实现在线天气预报">
<mx:Label x="28" y="32" text="选择城市" width="92" height="31"/>
<mx:ComboBox x="128" y="30" id="lang_select"></mx:ComboBox>
<mx:Button x="344" y="30" label="开始搜索" click="doSearch()"/>
<mx:TextArea id="output_text" x="28" y="71" width="386" height="247"/>
</mx:Panel>

</mx:Application>
proxy-config.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<service id="proxy-service" 
    class="flex.messaging.services.HTTPProxyService">    <properties>
        <connection-manager>
            <max-total-connections>100</max-total-connections>
            <default-max-connections-per-host>2</default-max-connections-per-host>
        </connection-manager>
        <allow-lax-ssl>true</allow-lax-ssl>
    </properties>    <adapters>
        <adapter-definition id="http-proxy" class="flex.messaging.services.http.HTTPProxyAdapter" default="true"/>
        <adapter-definition id="soap-proxy" class="flex.messaging.services.http.SOAPProxyAdapter"/>
    </adapters>    <default-channels>
        <channel ref="my-http"/>
        <channel ref="my-amf"/>
    </default-channels>    <destination id="DefaultHTTP">
    </destination>
    
    <destination id="mywebservice">
     <properties>
     <dynamic-url>
     http://www.webxml.com.cn/*
     </dynamic-url>
     </properties>
    </destination></service>
运行页面文件就可以了,呵呵。
休息了,有朋友遇到这样的问题希望好好看看这篇文章,有问题和我联系吧,QQ 94129171

解决方案 »

  1.   

    PS:在。net环境下,不需要配置proxy-config.xml这个的,只需要在<mx:WebService >标签里添加useProxy="false"就可以了
      

  2.   

    第二个天气预报的例子,我直接把destination干掉,编译swf,,,直接用IE打开,冇有问题,可以访问webservice哦。。
      

  3.   

    补充一下:修改xml文件后需要重新启动服务器
      

  4.   

    楼主的方法果然有效,对于HttpService也是一样的,学习了!
      

  5.   

    如果是datagird是怎么显示数据的呢?