【问题标题】:How to retrieve Data from Php while in Node.js在 Node.js 中如何从 Php 中检索数据
【发布时间】:2019-02-26 14:28:13
【问题描述】:

我只是想在我的应用程序开始时从用户那里检索一些 PHP/Mysql 内容(身份验证和 x 和 y 数据),然后我计划将这些内容发送到 app.js(开始时一次,一次用户断开更新x - y 值)。

所以基本上我已经设置了 Nodes.js 并且明白有些东西不像以前那样是不可能的(例如使用普通 php)

我已经遇到问题的地方是我的节点服务器的 index.html 中的 AJAX php 请求

架构:

app.js: 从/Client/index.html 拉取数据(我认为需要通过套接字来完成)

index.html:通过 Ajax 获取或发布数据到 php 文件,并将数据库的值返回到 index.html(JavaScript) 然后通过套接字将该数据发送到app.js

php:选择mysql数据库 从 mysql 中检索值 通过 Json 解析它们并使其在 index.html 文件 Nodes.js (Client) 中可用

也许你们中有人有解决办法

Nodes.js /Client/index.html:

function checkuser(username, password) {
    var myObj;
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            // Typical action to be performed when the document is ready:
            myObj = xhttp.responseText;
            var i = 0;
            while (i <= myObj.length) {
                //console.log("ASQL found and Auth Username:"+ myObj[i].username)  ;
                console.log(myObj.username);
                i++;
            }
        }
    };
    xhttp.open("GET", "/client/is_user.php?username333=" + username + "&password333=" + password, true);
    xhttp.send();
}

is_user.php:

<?php
require('config_sql.php');
$email = stripslashes($_GET['username333']);
$email = mysqli_real_escape_string($con,$email);
$password369 = stripslashes($_GET['password333']);
$password369 = mysqli_real_escape_string($con,$password369);
$query = "SELECT * FROM `users` WHERE email='$email'
and password='".md5($password369)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$response = array();
$rows = mysqli_num_rows($result);
while ($row_user= mysqli_fetch_assoc($result))
{
    $response[] = $row;
}
$jsonData = json_encode($response); 
echo $jsonData;
mysqli_close($con);
?>  

atm 没有从 php 端创建的 json 中检索用户名。 如果我console.log(myObj); 它向我显示完整的 php.file 数据作为纯文本,如果我想从 MySql 中检索用户名,它的说法是 undefined

当我在 Node.js 环境中通过 Ajax post/get 时,php 解释器是否真正工作? 通常,当我使用纯 php 编程时,所有请求都运行良好。

提前谢谢你。

【问题讨论】:

  • 为什么不在节点中做呢?
  • 警告: 不要使用md5() 进行密码散列。这是非常不安全的。使用 PHP 时,请改用 password_hash()password_verify()。在散列密码之前,您也不应该转义密码。这实际上改变了密码。
  • 您还应该在发送用户凭据时使用 POST 而不是 GET(因为它会在 URL 中以明文形式发送用户名和密码)。
  • 为什么不在节点中?我不想向所有人显示我的 mysql 登录数据,因为每个人都可以看到 javascript 源代码?
  • 节点是服务器端,因此人们将无法访问该代码。它只是人们可以看到的客户端 javascript,而这不是您放置任何 mysql 凭据的地方。

标签: javascript php json node.js ajax


【解决方案1】:

检查您的代码以从查询中接收结果:

$rows = mysqli_num_rows($result);
while ($row_user= mysqli_fetch_assoc($result))
{
$response[] = $row;
}

$jsonData = json_encode($response);

应该是这样的:

$rows = mysqli_num_rows($result);
while ($row_user= mysqli_fetch_assoc($result))
{
$response[] = $row_user;
}

$jsonData = json_encode($response);

【讨论】:

    猜你喜欢
    • 2011-10-26
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多