0

I'm trying to send a simple data {"DATA1":"111","DATA2":"222"} in json format to PHP from ActionScript 3, I saw this codes work for another user, but I don't know why it doesn't work in my system. I'm using XAMPP in local machine.

AS3:

import flash.net.*;
import flash.events.Event;

var request:URLRequest = new URLRequest("http://localhost/as_json_write_2.php")
var variables:URLVariables = new URLVariables()
    variables.DATA1 = "111111"
    variables.DATA2 = "222222"

request.data = variables
request.method = URLRequestMethod.POST
var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, handleComplete);
    loader.load(request)

function handleComplete(event:Event) {

    var loader:URLLoader = URLLoader(event.target)
    var vars:URLVariables = new URLVariables(loader.data)

    trace("vars.DATA1: "+vars.DATA1)
    trace("");
    trace("vars.DATA2: "+vars.DATA2)
} 

Animate CC Output:

vars.DATA1: 111111

vars.DATA2: 222222

PHP:

<?php

    $DATA1 = $_POST['DATA1'];
    $DATA2 = $_POST['DATA2'];

    echo "DATA1=".$DATA1;
    echo "&DATA2=".$DATA2;
?>

PHP messages:

Notice: Undefined index: DATA1 in C:\xampp\htdocs\as_json_write_2.php on line 3

Notice: Undefined index: DATA2 in C:\xampp\htdocs\as_json_write_2.php on line 4

DATA1=&DATA2=
Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
Alik Miankoli
  • 247
  • 1
  • 12
  • End your PHP script with 3rd line and see what goes as the source JSON string. If there's not a valid JSON string it might quietly fail if server warning/error settings are set to not report anything. – Organis Jan 26 '17 at 13:41
  • Change this line: request.data = postParams; to request.data = JSON.stringify(postParams); – Mr. Phantom Jan 26 '17 at 17:55
  • Replace your JSON_decode line with this: $rawData = file_get_contents("php://input"); var_dump($rawData); and tell us if rawData has any value. – Eric Jan 26 '17 at 19:13
  • It seems you need PHP help but can't help you if you can't debug it... – Eric Jan 26 '17 at 19:15
  • That means that PHP doesn't receive any data, and you either have an error in your AS3 or that your server isn't CORS. – Eric Jan 26 '17 at 20:19
  • There is no error in Animate CC, and in PHP side some Notice and "DATA1=DATA2=" – Alik Miankoli Jan 26 '17 at 20:26
  • @AlikMiankoli Try to add `&` before the `"DATA2=".$DATA2` in your PHP file: `echo "&DATA2=".$DATA2;` – rdleal Jan 26 '17 at 21:00
  • @PanterA. I added and edited codes here. – Alik Miankoli Jan 26 '17 at 21:05
  • @AlikMiankoli so you've got the same error messages? – rdleal Jan 26 '17 at 21:06
  • @PanterA. see them in "PHP messages:" end of my post, i edited it. – Alik Miankoli Jan 26 '17 at 21:08
  • @AlikMiankoli It seems to be working as expected. You Animate CC output is correct, as you sent the values in the POST request. The PHP output makes sense, since you are probably acessing the url `http://localhost/as_json_write_2.php` directly (i.e: via browser) and thus issuing a GET request, which doesn't fill the global variable `$_POST`. – rdleal Jan 26 '17 at 21:11
  • @PanterA I'm designer and animator, I really don't know details, you mean AS3 and PHP codes are true and there is another problem? – Alik Miankoli Jan 26 '17 at 21:14
  • @AlikMiankoli If you wanted to send data to PHP and retrieve the same data in the ActionScript, that's working now. Because of the `&` you added before the `DATA2`. – rdleal Jan 26 '17 at 21:23
  • @PanterA. and why there is no data? – Alik Miankoli Jan 26 '17 at 21:36
  • When i stop XAMP Animate CC show error, My codes now works but i don't know why PHP don't like to show data. – Alik Miankoli Jan 26 '17 at 21:48

0 Answers0