我真的無法弄清楚什麼是錯的。從AS3發送JSON到PHP
林發送JSON從AS3到PHP:
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;
}
您是否找到了解決方案?如果是的話,請發佈。 – 2013-04-25 11:18:16
糟糕的是PHP收到了錯誤的格式化JSON,因爲編譯器在特殊符號前添加了「\」符號。我已經使用** json_decode(stripslashes($ _ POST ['myObject'])); **在PHP中接收JSON並開始工作正常:)請看這裏:http://stackoverflow.com/questions/15943488/ json-format-lost-in-php – SzRaPnEL 2013-04-25 13:44:49
是的,我在1小時前做了同樣的工作。謝謝。 – 2013-04-25 14:01:17