从AS3发送JSON到PHP

时间:2013-04-09 11:31:24

标签: php json actionscript-3 variables

我真的无法弄清楚出了什么问题。

我从AS3向PHP发送JSON:

var sendToPHPJson:String = com.adobe.serialization.json.JSON.encode(sqlResult);

myRequest = new URLRequest("http://xxx.pl/FlashFiles/Winebook/uploadToServer.php");
myLoader = new URLLoader;
myVariables = new URLVariables;
myVariables.firstProperty = sendToPHPJson;
myLoader.addEventListener(flash.events.Event.COMPLETE,onUploadingComplete);
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
myRequest.method = URLRequestMethod.POST;
myRequest.data = myVariables;
myLoader.load(myRequest);

我的PHP正在接收:

<?php
include "XXX.php";
$json=$_POST['firstProperty'];
$data = json_decode($json);
$answe=$data[0]->wineName;
echo "answer='".$answe."'";
?>

我收到回答=''。但是当我将sendToPHPJson硬编码到PHP中时,答案具有很好的价值。

有什么不对?要改变什么?

*编辑: 我做了一些搜索并重建代码:

AS3:

var url:String = "http://adres/uploadToServer.php";
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.POST;

var requestVars:URLVariables = new URLVariables();
requestVars.myObject = sendToPHPJson;
request.data = requestVars;

var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE,onUploadingComplete);
loader.load(request);

PHP:

<?php
include "XXX.php";
ob_start();
var_dump($_POST['myObject']);
print_r($_POST);
$content = ob_get_contents();
ob_end_clean();
$wynik = json_decode($content);
echo $wynik;
?>

我仍然没有得到数据:

private function onUploadingComplete(e:flash.events.Event):void
{
    trace("upload complete");
    trace(e.target.data);
    txtField.text = String(e.target.data);
    this.touchable = true;
}

1 个答案:

答案 0 :(得分:3)

应该是

myRequest.data = myVariables;

此外,AS3内置了JSON顶级类,无需使用第三方库。下次检查var_dump($ _ POST)或浏览器网络信息,您的数据是如何发送的。