I am requesting this PHP page below (welcome.php) from my local php server.
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
I am requesting in my Nativescript application (main-page.js)
exports.postRequest = function() {
http.request({
url: "http://192.168.3.12:8888/php/welcome.php",
method: "POST",
content: JSON.stringify({ name: "test", email: "test@email.com"})
}).then(function(result) {
console.log("Success!");
console.log(JSON.stringify(result));
}, function(error) {
console.error(JSON.stringify(error));
});
}
Question(s):
I assume the problem is that it is looking for a JSON value to be returned? And the welcome.php is just creating two lines in the body of an html page. Should this automatically get parsed and converted into JSON? Is there a function that should return it to the requester? How can I edit this to get a value back into the .js file. Is my whole structure just off?
EDIT:
I mentioned in one of my comments to @Quentin that it doesn't matter whether I logged the 'result' as a plain object or not. This turned out to be untrue: if I just output the result without using JSON.stringify i get the following message:
JS: Success!
JS: Result (plain)[object Object]
I think it is still empty even after editing the welcome.php to be this:
<html>
<body>
<?php
$_POST = json_decode(file_get_contents('php://input'), true);
?>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
I think the issue lies with my input of the name & email vars in the main-page.js.
Edit 2
I went ahead and accept Woodrow's answer because it is getting me closer to a solution. I still have a bit more to look through. This post may have an answer for me. Thank you everyone for contributing!
Solution Edit:
I was able to (with help from everyone) to figure this disconnect out between my PHP on the server and .js file in Nativescript client. Thanks @Quentin, @Woodward, and @ MarekMaszay to name a few.
Here are the final files: jsonWelcome.php
<?php
$_POST = json_decode(file_get_contents('php://input'), true);
$name = $_POST['name'];
$email = $_POST["email"];
//This outputs to a log in the server specified in the php.ini. I used it
// to see if/what the server was receiving from the .js file. I finally got
// that part down & moved on to outputting the data as JSON below.
error_log("name: " . $name . " email:". $email);
if ( $name == "taylor"){
$data = "YESSSS";
} else {
$data = "NOOO";
}
header('Content-type: application/json'); //I haven't tested without this.
exit (json_encode( $data)); //I needed to use the exit & json_encode together
?>
app/main-page.js (nativescript)
//Note I changed the 'result' to 'data'. I don't think it actually has any impact. I made other major changes.
exports.postRequest = function() {
http.request({
//url: "https://httpbin.org/post",
//url: "https://httpbin.org/post
url: "http://192.168.3.12:8888/php/jsonWelcome.php",
method: "POST",
content: JSON.stringify({name: "testNS", email: "email@nativescript.com"})
}).then(function(data) {
console.log("Success!");
console.log("Result (plain)" + data);
console.log("Result 0" + data[0]);
console.log("Result (json.stringify) " + JSON.stringify(data));
}, function(error) {
console.log("Failure");
console.error(JSON.stringify(error));
});
}
The result of the two above files will result in the following output from the console when run by clicking on the application.
JS: Result (json.stringify) {"content":"NOOO","statusCode":200,"headers":{"null":"HTTP/1.1 200 OK","Connection":"close","Content-type":"application/json","Date":"Fri, 28 Jul 2017 11:15:55 -0600","Host":"192.168.3.12:8888","X-Android-Received-Millis":"1501262155358","X-Android-Response-Source":"NETWORK 200","X-Android-Selected-Protocol":"http/1.1","X-Android-Sent-Millis":"1501262155342","X-Powered-By":"PHP/7.1.7"}}
Notice that content is equal to "NOOO" which is what we want in this case. Now that I have the basic data exchange down, I am happy. Thanks so much!
Oh and p.s. I think one of the main issues aside from editing the input & output was that I added php tags:
<?php ?>
to my jsonWelcome.php file. I had completely missed this. Although there were other major issues, this was a major blindspot for me that was easily fixed with closer inspection.
Thanks!