doucheng5705 2016-06-11 08:11
浏览 42
已采纳

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

I am running an apache server from a Raspberry Pi and have a python script that returns sensor input by printing it. 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. This also works when run from the console. The last part is javascript that is supposed to get the output from the php. It does that using ajax, which runs the "success" function, but gets "0" from the php script.

My php script:

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

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

  • 写回答

1条回答 默认 最新

  • ds3016 2016-06-13 08:32
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog