为什么php文件不响应json,但它响应连接文件。
文件回复
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include '../connect.php';
mysqli_set_charset($conn, "utf8");
$sql="SELECT confer_name, confer_begin_time, confer_end_time, confer_value, con_img FROM conference";
$outp = "";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0){
while($rs = $result->fetch_array(MYSQLI_ASSOC)){
if($outp != ""){$outp .=",";}
$outp .='{"cname":"' .$rs["confer_name"]. '",';
$outp .='"cbt":"' .$rs["confer_begin_time"] . '",';
$outp .='"cet":"' .$rs["confer_end_time"] . '",';
$outp .='"cvalue":"' .$rs["confer_value"] . '",';
$outp .='"cimg":"' .$rs["con_img"] . '"}';
}
$outp = '{"records":['.$outp.']}';
echo $outp;
}
?>//***
文件连接此文件用于连接。
<?php
$servername = "localhost";
$username = "----";
$password = "----";
$database = "----";
$conn = new mysqli($servername,$username,$password,$database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
答案 0 :(得分:0)
修改您的代码,如下所示
$outp = json_encode($outp);