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 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?