javascript到PHP的通信问题

javascript到PHP的通信问题,javascript,php,html,Javascript,Php,Html,好的,我有一个网站,可以远程控制Raspberry Pi上的GPIO通用输入/输出引脚,它正在使用库WiringPi运行。问题是,在javascript和php代码之间,HTTPrequest.responseText总是返回未定义的。index.php读取管脚并打印其状态: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Raspberry P

好的,我有一个网站,可以远程控制Raspberry Pi上的GPIO通用输入/输出引脚,它正在使用库WiringPi运行。问题是,在javascript和php代码之间,HTTPrequest.responseText总是返回未定义的。index.php读取管脚并打印其状态:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Raspberry Pi Home Automation</title>
</head>

<body style="background-color: black;">
 <?php 

// Define your username and password 
$username = "[not giving you my username]"; 
$password = "[or password]"; 

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { 

?> 

<h1 style="color: white">Login</h1> 

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
<p style="color: white"><label for="txtUsername">Username:</label> 
<br /><input type="text" title="Enter your Username" name="txtUsername" /></p> 

<p style="color: white"><label for="txtpassword">Password:</label> 
<br /><input type="password" title="Enter your password" name="txtPassword" /></p> 

<p><input type="submit" name="Submit" value="Login" /></p> 

</form> 

<?php 

} 
else { 

?> 
 <?php
 //this php script generate the first page in function of the gpio's status
 $status = array (0, 0, 0, 0, 0, 0, 0, 0);
 for ($i = 0; $i < count($status); $i++) {
    //set the pin's mode to output and read them
     if ($i != 5){
      system("gpio mode ".$i." out");
     } else {
      system("gpio mode ".$i." in");
     }
    exec ("gpio read ".$i, $status[$i], $return );
    //if off
    if ($status[$i][0] == 0 ) {
    echo ("<img id='button_".$i."' src='data/img/off/off_".$i.".jpg' alt='off'/>");
    }
    //if on
    if ($status[$i][0] == 1 ) {
    echo ("<img id='button_".$i."' src='data/img/on/on_".$i.".jpg' alt='on'/>");
    }    
 }
 ?>
<?php 


} 

?>

 <!-- javascript -->
 <script src="script.js"></script>
</body>
除此之外,所有其他事件侦听器函数都与上面的函数相同:

button_5.addEventListener("click", function () { 

var new_status = change_pin ( 5, 0);

if (new_status == "yes") 
{ 
    button_5.alt = "on"
    button_5.src = "data/img/on/on_5.jpg"; 
    return 0;
} 
else if (new_status == "no")
{
    button_5.alt = "off"
    button_5.src = "data/img/off/off_5.jpg";
    return 0;
}
else if (new_status == "fail"){
   return 0; 
} else {
 alert("return fail " + new_status);   
}
} );
我让php代码返回一个表示我所创建状态的字符串,以消除类型问题的可能性:它不起作用。以下是gpio.php:

<!-- This page is requested by the JavaScript, it updates the pin's status and then print it -->
<?php
//Getting and using values
if (isset ($_GET["pin"]) && isset($_GET["status"]) ) {

$pin = strip_tags($_GET["pin"]);
$status = strip_tags($_GET["status"]);

//Testing if values are numbers
if ( (is_numeric($pin)) && (is_numeric($status)) && ($pin <= 7) && ($pin >= 0) && ($status ==     "0") || ($status == "1") ) {
    if ($pin != 5){
        //set the gpio's mode to output     
        system("gpio mode ".$pin." out");

        //set the gpio to high/low
        if ($status == "0" ) {
            $status = "1";
        }
        else if ($status == "1" ) {
            $status = "0";
        }
        system("gpio write ".$pin." ".$status );
    }

    //reading pin's status
    $status = array (0, 0, 0, 0, 0, 0, 0, 0);
    for ($i = $pin; $i == $pin; $i++) {
        exec ("gpio read ".$i, $status[$i], $return );

        if ($status[$i][0] == 0 ) {
            echo ("no");
        } else if ($status[$i][0] == 1 ) {
            echo ("yes");
        } else {
            echo ("fail");
        }
    }

} 
else {
    echo ("fail");
}
} //print fail if cannot use values
else {
echo ("fail");
}
?>

我甚至创建了一个只运行一次的冗余for循环,以确保变量$status正确返回。据其他人说,我已经用尽了唯一的可能。在这一点上,任何批评都是有帮助的,提前感谢您,感谢您忍受了这么多代码。

exec gpio read.$i、$status[$i]、$return

.i后面的逗号不应该是下面的句点吗


执行gpio读取。$i$状态[$i],$return

有控制台错误吗?当从位置栏而不是脚本运行时,PHP是否返回正确的内容,wiringpi库完全按照开发人员所说的做了:返回一个整数1或0,表示pin的状态。我的印象是,status是发送给exec函数的命令中的第二个参数:第一个是带有命令的字符串。
<!-- This page is requested by the JavaScript, it updates the pin's status and then print it -->
<?php
//Getting and using values
if (isset ($_GET["pin"]) && isset($_GET["status"]) ) {

$pin = strip_tags($_GET["pin"]);
$status = strip_tags($_GET["status"]);

//Testing if values are numbers
if ( (is_numeric($pin)) && (is_numeric($status)) && ($pin <= 7) && ($pin >= 0) && ($status ==     "0") || ($status == "1") ) {
    if ($pin != 5){
        //set the gpio's mode to output     
        system("gpio mode ".$pin." out");

        //set the gpio to high/low
        if ($status == "0" ) {
            $status = "1";
        }
        else if ($status == "1" ) {
            $status = "0";
        }
        system("gpio write ".$pin." ".$status );
    }

    //reading pin's status
    $status = array (0, 0, 0, 0, 0, 0, 0, 0);
    for ($i = $pin; $i == $pin; $i++) {
        exec ("gpio read ".$i, $status[$i], $return );

        if ($status[$i][0] == 0 ) {
            echo ("no");
        } else if ($status[$i][0] == 1 ) {
            echo ("yes");
        } else {
            echo ("fail");
        }
    }

} 
else {
    echo ("fail");
}
} //print fail if cannot use values
else {
echo ("fail");
}
?>