POST parameters to php

Hello guys,

I am looking for several hours now, but I cant get a solution, to pull a json object back from a php page which I passed to it before that.

This is my code so far

Javascript in mainview.ux

        var Observable = require('FuseJS/Observable');
        var status = 0;
        var response_ok = false; 
        var name = Observable("loading...");
        var name2 = Observable("");

        var requestObject = {"name":12,"name2":10}; 

           console.log(JSON.stringify(requestObject));
        fetch('http://192.168.0.12/index.php', {            method: 'POST',
            headers: { "Content-type": "application/json"},
            dataType: 'json', 
            body: JSON.stringify(requestObject)
        }).then(function(response) {            status = response.status;  // Get the HTTP status code
            response_ok = response.ok; // Is response.status in the 200-range?

            return response.json();    // This returns a promise
        }).then(function(responseObject) {
            name.value = responseObject.name;
            name2.value = responseObject.name2; 
        }).catch(function(err) {
            // An error occured parsing Json
            console.log(err.message);        });
        module.exports = {
        };

php file

    <?php

        if($_SERVER['REQUEST_METHOD']=='POST') {


        $jsonData = "php://input";
        $phpJsonString = file_get_contents( $jsonData );
        $data = json_decode( $phpJsonString ); 

        $var = json_encode($data, JSON_FORCE_OBJECT);

        echo($var);
        }
    ?>

and the console reaction to it…

LOG: Unexpected token <
LOG: Unexpected token <
LOG: Unexpected token <
LOG: Unexpected token <

If I have an array in the php file and want to read it only, it works. But not when I try to pass it from my app.

Have I missed something?

Could you do the request in your web browser or using something like wget, and show how the response from the php server looks?

I made a small test script, it looks like this

<?php

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

$data=json_encode($array, JSON_FORCE_OBJECT); 
$encoded = htmlentities($data);

echo '
<form method="POST" action="index.php">

<input type="hidden" name="array" value="'.$encoded.'">
<input type="submit" name="submit" value="go!" />

</form>';

?>

and changed my actual php script to this

<?php

if($_SERVER['REQUEST_METHOD']=='POST') {


$jsonData = "php://input";
$phpJsonString = file_get_contents( $jsonData );
$data = json_decode( $phpJsonString );

echo($_POST['array'] . "<br>");

$var = json_encode($data, JSON_FORCE_OBJECT);

echo($var);
}
?>

The echo post with break is made to test if there is an output at all. The output is valid json, but this doesnt work with fuse post. In the second line the output of $var is null, $data shows nothing and $phpJsonString shows weird numbers and html structures.

{"foo":"bar","bar":"foo"}
null