简体   繁体   中英

Sending JSON from AS3 to PHP

I really can't figure out what's wrong.

Im sending JSON from AS3 to 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);

And my PHP is receiving:

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

And i receive answer=''. But when I hardcode sendToPHPJson into PHP answer has good value.

What can be wrong? What to change?

*EDIT: I did some search and rebuild code to:

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;
?>

And I still get no data in:

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

It should be

myRequest.data = myVariables;

Also, AS3 has built-in JSON top level class, no need to use 3rd party libraries. Next time check var_dump($_POST) or browser network info, how's your data is being sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM