javascript - 更改 PHP 代码以返回 JSON 值

标签 javascript php json nativescript

我正在从本地 PHP 服务器请求下面的 PHP 页面 (welcome.php)。

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

我在我的 Nativescript 应用程序 (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));
    });
}

问题:

我认为问题是它正在寻找要返回的 JSON 值? welcome.php 只是在 html 页面的主体中创建两行。是否应该自动解析并转换为 JSON?是否有一个函数可以将其返回给请求者?我如何编辑它才能将值返回到 .js 文件中。我的整个结构就这样结束了吗?

编辑:

我在对 @Quentin 的评论中提到,我是否将“结果”记录为普通对象并不重要。事实证明这是不正确的:如果我只是输出结果而不使用 JSON.stringify 我会收到以下消息:

JS: Success!
JS: Result (plain)[object Object]

即使将welcome.php编辑为这样,我认为它仍然是空的:

<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>

我认为问题在于我在 main-page.js 中输入的姓名和电子邮件变量。

编辑2

我继续接受伍德罗的回答,因为它让我更接近解决方案。我还有一些事情需要看。 This post可能会给我一个答案。感谢大家的贡献!

解决方案编辑:

我能够(在大家的帮助下)找出服务器上的 PHP 和 Nativescript 客户端中的 .js 文件之间的这种脱节。感谢 @Quentin、@Woodward 和 @MarekMaszay 等。

以下是最终文件: 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(原生脚本)

//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));
    });
}

当单击应用程序运行时,上述两个文件的结果将在控制台中产生以下输出。

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"}}

请注意,内容等于“NOOO”,这正是本例中我们想要的。现在我已经完成了基本的数据交换,我很高兴。非常感谢!

哦,还有 p.s。我认为除了编辑输入和输出之外的主要问题之一是我添加了 php 标签: <?php ?>

到我的 jsonWelcome.php 文件。我完全错过了这一点。尽管还有其他重大问题,但这对我来说是一个主要盲点,通过仔细检查很容易修复。

谢谢!

最佳答案

您需要对发布的数据进行 json_decode.. 因为您将其作为 JSON 有效负载发送到服务器。要获取 POST 数据,您必须执行以下操作:

$_POST = json_decode(file_get_contents('php://input'), true);

关于javascript - 更改 PHP 代码以返回 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45355985/

相关文章:

javascript - Ajax回调中的if语句

javascript - Ajax 总是给出积极的值(value)。如何纠正呢?

javascript - 使用 Onclick 赋值的 For 循环中的闭包

php - 如何在 Wamp 上安装 simplesaml 作为 SP

javascript - 当输入文本被清除时,如何隐藏ajax搜索结果?

php - 如何从 PHP 执行 SSH?

javascript - 使用嵌套映射操作从数组返回对象

java - jackson 在通用列表中读取 json

c# - Newtonsoft json.net反序列化NullReferenceException

php - 限制包含 HTML 标签的文本的输入长度