php没有返回JSON对象
Fullcalendar events.php not returning a JSON object
我试图发送一个JSON对象回到我的fullcalendar ajax请求,而不是它只是返回一个数组。我是JSON和PHP的新手,但我已经做了尽可能多的研究,并没有发现任何有用的。
events.php
<?php
// List of events
$json = array();
// Query that retrieves events
$requete = "SELECT * FROM `evenement` ORDER BY `id`";
// connection to the database
require_once 'mysqli_connect.php';
// Execute the query
$resultat = $dbc->query($requete) or die(print_r($dbc->errorInfo()));
// sending the encoded result to success page
$tempjson = json_encode($resultat->fetch_all(PDO::FETCH_ASSOC));
$tempjson = str_replace('"false"', 'false', $tempjson);
echo $tempjson;
?>
返回的数据
[["1","title","2015-07-26 00:00:00","2015-07-26 00:00:00","","1"]]
所以,我的问题是,为什么它不是在一个正确的JSON键/值对格式返回?
我不认为问题是与jQuery,但只是以防万一…
来自calendar.js的ajax调用
eventSources: [
{
url: '/php/events.php',
type: 'GET',
data: {},
error: function (data) {
alert('There was an error while fetching events!');
console.log(data);
}
}
],
我认为,解决方案是循环结果从mysql和生成所需的数组。基本想法是确保您的.php
根据完整日历的要求生成有效的JSON对象。eventSources的可接受值在它们的文档中有提及
// Query that retrieves events
$requete = "SELECT * FROM `evenement` ORDER BY `id`";
// connection to the database
require_once 'mysqli_connect.php';
// Execute the query
$resultat = $dbc->query($requete) or die(print_r($dbc->errorInfo()));
// sending the encoded result to success page
while($row = $resultat->fetch_assoc()){
$array[] = $row;
}
$tempjson = json_encode($array);
$tempjson = str_replace('"false"', 'false', $tempjson);
echo $tempjson;
?>
我认为你使eventSources
复杂化了。试试简单的:
eventSources: [
'/php/events.php'
]
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 如何为json对象中的段发送array[]
- 将JSON对象传递给angular指令
- 更改JSON对象的结构
- 访问JSON对象内部的数组元素
- 在ejs-partial中对JSON对象进行迭代
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 访问嵌套JSON对象的键,其中键是动态的
- json对象中缺少对象循环
- 发送json对象或使用express路由呈现视图
- 在play2框架中向json对象添加下拉列表项
- 元素名称上带有短划线 (-) 字符的 Json 对象
- autocomplete不接受源的json对象
- 如何在javascript中创建动态json对象
- 在使用客户端脚本时拾取JSON对象
- 如何通过json对象数组为嵌套对象赋值
- 构造JSON对象