[code]
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.events.MouseEvent;
import fl.controls.ComboBox;
/**
* ...
* @author amyflash.com
*/
public class Main extends Sprite
{
private var ul:URLLoader = new URLLoader();
private var ua:URLRequest = new URLRequest();
private var ak:String = "baidu_ak"; //换成你自己的
//private var sn:String = "baidu_sn";
public function Main()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
sb.addEventListener(MouseEvent.CLICK,doSb);
}
private function doSb(e:MouseEvent):void{
ua.method = URLRequestMethod.POST; //.method 也为 URLLoader的一个属性值
var urlvariables:URLVariables = new URLVariables(); //建立URLVariables对象,
urlvariables.ak = ak; //通过cc参数传递 txtcontent里的数据
// urlvariables.sn = sn;
//urlvariables.q = "医院";//encodeURIComponent(ti.text);
//urlvariables.region =encodeURIComponent(cb.selectedItem.label);
ua.data = urlvariables;//讲urlvariables的数据赋值给.data
ua.url = "http://api.map.baidu.com/place/v2/search?q="+ti.text+"&region="+cb.selectedItem.label+"&output=json";
//ua.url = "http://api.map.baidu.com/place/v2/search?output=json";
ul.load(ua);
ul.addEventListener(Event.COMPLETE, showData);
}
private function showData(e:Event):void{
trace(e.currentTarget.data);
re.text = e.currentTarget.data;
}
}
}
[/code]
[code]
//利用php中转,解决跨域问题
<?php
$fd1 = $_POST['message_q'];
$fd2 = $_POST['message_r'];
$homepage = file_get_contents('http://api.map.baidu.com/place/v2/search?q='.$fd1.'®ion='.$fd2.'&output=json&ak=BAfb9bb97f7b712e5426859ac957c98f');
echo $homepage;
?>
[/code]
或者用jsonp解决跨域问题,参考 http://tieba.baidu.com/p/2467953456