Jquery+php+串行通信
我在通过下面的ajax调用获取返回值时遇到了麻烦。函数正在将参数发送到php文件,并且工作正常,但是没有得到返回值Jquery+php+串行通信,php,jquery,ajax,Php,Jquery,Ajax,我在通过下面的ajax调用获取返回值时遇到了麻烦。函数正在将参数发送到php文件,并且工作正常,但是没有得到返回值 $(document).ready(function(){ $state = "greenoff"; $('input[type="checkbox"]').click(function() { if($(this).is(":checked"
$(document).ready(function(){
$state = "greenoff";
$('input[type="checkbox"]').click(function()
{
if($(this).is(":checked")){
$state = "greenon";
}
else if($(this).is(":not(:checked)")){
$state = "greenoff";
}
$.ajax({
type: "POST",
url: "led.php",
data: { 'action': $state }
})
.done(function(msg) {
alert( "Data Saved: " + msg );
});
});
}
);
我的php代码如下
<?php
if (isset($_POST['action'])) {
require("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0");
$serial->confBaudRate(9600); //Baud rate: 9600
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1")
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1")
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1")
$serial->confFlowControl("none");
$serial->deviceOpen();
sleep(2);
if ($_POST['action'] == "greenon") {
$serial->sendMessage("H\r");
$read = $serial->readPort();
echo $read;
} else if ($_POST['action'] == "greenoff") {
$serial->sendMessage("L\r");
$read = $serial->readPort();
echo $read;
}
}
?>
将您的PHP文件分成多个部分,并在PHP文件的各个点上使用die'I am here 01'等将其终止 这将让你看到你正在走多远,并确定问题的确切起点 第一个测试只是将PHP代码替换为:
<?php
die('Got to here');
您的AJAX代码块应该响应该消息。如果没有,那么ajax根本就不会发生