How do I receive POST data in PHP sent from Fuse->JavaScript by fetch()???

You guys have done an absolute awesome job by creating Fuse and how easy it is to use with common procedures one want in an app, so therefore I have to thank you! However, I find it unbelievably frustrating when it comes to communication with my MySQL server through PHP scripts using JavaScript’s fetch() in Fuse. I can say I have experience with using PHP scripts on the server to receive and send back data with POST and JSON (C#) but this is just nothing like that. I have tried sooo many different combinations now the whole evening off of your Forums and by investigating JavaScript API but no luck so far and it’s starting to get really frustrating. I have managed to establish communication with my .php files on the server (i.e. just echoing random stuff and receive them in Fuse) but I have no idea how either send POST data or receive them in PHP with fetch() in JS… Could anyone please help me?

My .js file:

var Observable = require('FuseJS/Observable');

var Email = Observable();
var Password = Observable();

var fullname = Observable("7t978y9");

function logIn() {
	
	var status = 0;
	var response_ok = false;

	var requestObject = {email: Email.value, password: Password.value};

	console.log(requestObject);

	fetch('http://www.myserver.com/sql/json/get_user.php', {
		method: 'POST',
		headers: { "Content-type": "application/x-www-form-urlencoded"},
		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

	    console.log("1st then");

	}).then(function(responseObject) {

		fullname.value = responseObject;
		console.log("2nd then");
	console.log(fullname.value);

	}).catch(function(err) {

		console.log("Fetch error: " + err);
	});

}

module.exports = {

	Email: Email,
	Password: Password,
	fullname: fullname,
	logIn: logIn

};

My .php file:

<?php
    require_once "../app_config.php";

    $email = $_POST["email"];
    $password = md5($_POST["password"]);

    if (!$db) {
        echo "No Connection";
    }
    else
    {
        $query = "select * from users where email = '$email' and password = '$password'";

        $get_user = $db->query($query);

        foreach ($get_user as $user) {
            echo json_encode($user);
        }
    }

I’m desperate…

So, what is happening? Are you getting an error message? If so, what message?

Thanks for the fast reply!
Sorry I didn’t mention.

With this code it just says that the $_POST[] variables are null in the PHP script and no error.

From error.log:

[09-Jan-2017 09:21:54 Europe/Oslo] PHP Notice:  Undefined index: email in /home/4/h/hangout/www/sql/json/get_user.php on line 4
[09-Jan-2017 09:21:54 Europe/Oslo] PHP Notice:  Undefined index: password in /home/4/h/hangout/www/sql/json/get_user.php on line 5

You are trying to post urlencoded form data, but you are sending it as JSON. You need to do someting like this to convert your Javascript object:

    function formEncode(obj) {
        var str = [];
        for(var p in obj)
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    }

    fetch('http://www.myserver.com/sql/json/get_user.php', {
        method: 'POST',
        headers: { "Content-type": "application/x-www-form-urlencoded"},
        body: formEncode(requestObject)
    })

That worked!
Thank you very much.

Hi, I am also building app with login and I am wondering how to stay logged in after successful login.
For example, checkbox “stay logged in”. How to store that information on local storage? What is the best approach for this?

Thanks

@solomun.marko: prefer making new forum posts for unrelated questions.

In short: your backend should reply with a session token upon a successful authorisation. The app should save that token in, for example, a JSON file using FileSystem module or as a key:value pair using fuse-usersettings library. And then, in your app startup routine, you should check if the token is present, and ask the server if the token is valid.

Sorry for a kind of unrelated question.
Thanks for pointing me in right direction. I’ll try with usersettings library. I know how to do it in Cordova, but not in Fuse.
It’s time for action :slight_smile:
Thanks!

Thank you so much! Love you))