<?php
$con=mysqli_connect("localhost","secret","secret","quakealert");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['id'])){
$id = $_POST['id'];
$name = areaName($id);
$data = statsReport($name);
//sleep(3);
echo json_encode($data);
//echo $data;
}
if(isset($_POST['idd'])){
$id = $_POST['idd'];
$name = areaName($id);
$data = UpdateChart($name);
echo json_encode($data);
}
function areaName($id){
$query = "SELECT area_name FROM `location_details` WHERE `Location_ID`='".$id."'";
$result = mysqli_query($GLOBALS['con'],$query);
$data = mysqli_fetch_array($result);
return $data[0];
}
function statsReport($name){
$query = "(SELECT * FROM `".$name."` ORDER BY id DESC LIMIT 4) ORDER BY id ASC";
$result = mysqli_query($GLOBALS['con'],$query);
$results_array = array();
while ($row = $result->fetch_assoc())
{
$results_array[] = $row;
}
return $results_array;
}
?>
这是我输出的PHP脚本,它是有效的JSON:
[{"id":"12","current_value":"3.6","last_value":"3","inputTime":"2016-08-10 17:16:02","updateTime":"2016-08-10 14:46:11","updateCheck":"1"}]
我的PHP脚本:
<script>
var ajaxCall = function(area_id){
return $.ajax({
type: "POST",
url: "test.php",
//dataType: 'application/json',
data:({id:area_id}),
});
}
var clifton = ajaxCall(3);
var defence = ajaxCall(1);
//parse the data of ajax and through it into chart show
clifton.success(function(data){
//alert(data[0][1]);
var testing = $.parseJSON(data);//SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
//data = JSON.parse(data);i tried this also
chartShow(testing,"clifton",3);
});
defence.success(function(data){
// data = JSON.parse(data);
chartShow(data,"defence",1);
});
</script>
它在我的本地主机上工作得非常好但是当我在godaddy域上传它时它显示了这个错误。