简体   繁体   English

如何在没有cgi的情况下从python到JavaScript的浮点数

[英]How to get a float from python to JavaScript without cgi

I am running an apache server from a Raspberry Pi and have a python script that returns sensor input by printing it. 我正在从Raspberry Pi运行一个apache服务器,并有一个python脚本,通过打印返回传感器输入。 This properly prints it to the console. 这会将其正确打印到控制台。 Currently I have a php script that gets this output and shuts off a light if the sensor is reading is high, before printing it again. 目前我有一个PHP脚本获取此输出并在传感器正在读取时关闭灯光,然后再次打印。 This also works when run from the console. 从控制台运行时也可以使用。 The last part is javascript that is supposed to get the output from the php. 最后一部分是javascript,它应该从php获取输出。 It does that using ajax, which runs the "success" function, but gets "0" from the php script. 它使用ajax,它运行“success”函数,但从php脚本获取“0”。

My php script: 我的PHP脚本:

<?php

exec ("python temp.py", $temp);
if((float)$temp[0]>28)
{
exec("gpio read 0",$state);
if($state[0]=="1")
{
include('gpio.php');
echo("Overheated: ".$temp[0]);
}
}
else
{
echo($temp[0]);
}
?>

My js: 我的js:

$.ajax(
    {
        type: "GET",
        url: "temp.php",
        dataType: "text",
        success: function(msg)
        {
            alert("asd"+typeof msg);
        document.getElementById('text').innerHTML = msg;
            return msg;
        },
    error: function(jqXHR, textStatus, errorThrown){
                alert(jqXHR.responseText);
            }

});

Any suggestions are greatly appreciated. 任何建议都非常感谢。 Thank you. 谢谢。

Right. 对。 It's probably user permission issues, because you get the output running script under your user from terminal. 这可能是用户权限问题,因为您从终端获取用户下的输出运行脚本。 And browser, which is running it under server's user, returns an empty string. 并且在服务器用户下运行它的浏览器返回一个空字符串。

Check www-data (or whatever) permissions when running script from your server. 从服务器运行脚本时检查www-data (或其他)权限。 Set error reporting and run: 设置错误报告并运行:

ini_set('display_errors', true);
error_reporting(E_ALL);

echo exec('whoami');

If it prints out user who is not sudoer, set correct permissions to allow your .py script execution by that user. 如果它打印出不是sudoer的用户,请设置正确的权限以允许该用户执行.py脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM