我尝试用Ajax调用php函数。这是我的html文件中的JavaScript代码:
<script type="text/javascript">
function ajax(){
$.ajax({
type:"POST",
url: "SQLCommunication.php",
dataType: "JSON",
success : function(json){
json = jQuery.parseJSON(json);
alert(json.value);
}
}
)
}
$("#btn_refresh").click(function(){
ajax();
});
</script>
我不知道是否必须指定我真正想要调用哪个PHP函数?我也不知道我是怎么做到的。
我的PHP功能:
header('Content-Type: application/json');
function readValue(){
$conn = establishConnection();
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT datetime, value FROM tempvalues";
$result = $conn->query($sql);
if($result->num_rows > 0){
$row = $result->fetch_assoc();
$arr["datetime"] = $row["datetime"]; //return datetime and value as array
$arr["value"] = $row["value"];
if(is_ajax()){
return json_encode($arr);
} else {
return $arr;
}
}
$conn->close();
}
现在问题是,如果我按下按钮就没有任何反应。
答案 0 :(得分:1)
如果你想在你的ajax响应中看到结果,你必须在控制器中使用echo()或任何其他打印方法而不是返回
答案 1 :(得分:1)
我会改写自己的风格
<强>的jQuery 强>
<script type="text/javascript">
$("#btn_refresh").click(function(){
$.ajax({
type:"POST",
url: "SQLCommunication.php",
dataType: "JSON",
success : function(data){
console.log(data);
if(data.status === "success"){
alert("success");
}else{
alert("error");
}
}
error : function(XHR, status){
alert("fatal error");
}
})
});
</script>
<强> PHP 强>
header('Content-Type: application/json');
function readValue(){
$conn = establishConnection();
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT datetime, value FROM tempvalues";
$result = $conn->query($sql);
if($result->num_rows > 0){
$row = $result->fetch_assoc();
$arr["datetime"] = $row["datetime"]; //return datetime and value as array
$arr["value"] = $row["value"];
$arr["status"] = "success";
}else{
$arr["status"] = "error";
}
return json_encode($arr);
$conn->close();
}
echo readValue();
未测试
更新
的functions.php
function readValue(){
$conn = establishConnection();
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT datetime, value FROM tempvalues";
$result = $conn->query($sql);
if($result->num_rows > 0){
$row = $result->fetch_assoc();
$arr["datetime"] = $row["datetime"]; //return datetime and value as array
$arr["value"] = $row["value"];
$arr["status"] = "success";
}else{
$arr["status"] = "error";
}
return json_encode($arr);
$conn->close();
}
function writeValue(){
...
}
SQLCommunication.php
header('Content-Type: application/json');
if(!isset($_GET['func']) && empty($_GET['func'])){
//make the file inaccessible without $_GET
$arr['status'] = "error";
echo json_encode($arr);
exit();
)
if($_GET['func'] === "readvalue"){
echo readValue();
}elseif($_GET['func'] === "writevalue"){
echo writeValue();
}elseif($_GET['func'] === "whatever"){
//whatever...
}
....
的jQuery
$("#btn_refresh").click(function(){
$.ajax({
type:"POST",
url: "SQLCommunication.php?func=readvalue", //SQLCommunication.php?func=writevalue
dataType: "JSON",
success : function(data){
console.log(data);
if(data.status === "success"){
alert("success");
}else{
alert("error");
}
}
error : function(XHR, status){
alert("fatal error");
}
})
});