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?