所以我为每个从这个数据库打印“local”列的函数都有一个:
https://i.imgur.com/IzKOzkt.png
直到这里一切顺利,这就是我得到的:
https://i.imgur.com/WH7d4uh.png
现在我想通过邮寄发送变量,当用户点击不同的菜单项时,我已经尝试了一切,但我无法让它工作。这是我的代码:
<?php
$query = "SELECT * FROM credenciais_sensores where ambiente = '1'";
$results = mysqli_query($conn, $query);
foreach ($results as $result){
$local = $result['local'];
$local = substr($local,0,7);
echo "<li><a class='clsPostData' data-oxiid='".$result['oxi_sensorid']."' data-oxikey='".$result['oxi_apikey']."' data-redoxid='".$result['redox_sensorid']."' data-redoxkey='".$result['redox_apikey']."' href='#'>".$local."</a></li>";
}
?>
这个工作正常,菜单正在打印我想要的,但现在我无法发布我想要的数据,如“oxi_sensorid”等...这是我的javascript:
<script type="text/javascript">
$(function(){
$('.clsPostData').click(function(e){
e.preventDefault();
var objPost = {};
objPost.oxiid = $(this).data('oxiid');
objPost.oxikey = $(this).data('oxikey');
objPost.redoxid = $(this).data('redoxid');
objPost.redoxkey = $(this).data('redoxkey');
$.ajax({
url: 'getObjects.php',
type: 'post',
data: objPost
}).done(function(responseFromPhp){
//Do something with the response, like
alert(responseFromPhp.message);
});
});
});
</script>
我的getObjects.php文件:
<?php
$oxiid = $_POST["oxiid"];
$oxikey = $_POST["oxikey"];
$redoxid = $_POST["redoxid"];
$redoxkey = $_POST["redoxkey"];
$response["message"] = "Grettings from php, we receive your data: ".$oxiid . $oxikey . $redoxid . $redoxkey;
echo json_encode($response);
?>
但是当我点击任何菜单项时,我总是得到弹出窗口“undefined”...有什么帮助吗?
答案 0 :(得分:1)
我认为那里的问题是你需要将dataType设置为json。尝试在您的ajax代码dataType: 'json'
之后添加type: 'post'
:
$.ajax({
url: 'getObjects.php',
type: 'post',
dataType: 'json', // add this
data: objPost
}).done(function(responseFromPhp){
//Do something with the response, like
alert(responseFromPhp.message);
});