Php Ajax结果以文本形式返回,并将其用作CLNDR.js中JSON数组的值,但它不起作用
我对这个问题的合适标题感到困惑,无论如何,让我解释一下我的问题是什么。我目前正在使用一个插件,我正在尝试使用数据库中的数据(生日)在我的日历中显示事件。我的php查询中的结果是预先格式化的 我的PHP是这样的:Php Ajax结果以文本形式返回,并将其用作CLNDR.js中JSON数组的值,但它不起作用,php,jquery,ajax,json,jquery-plugins,Php,Jquery,Ajax,Json,Jquery Plugins,我对这个问题的合适标题感到困惑,无论如何,让我解释一下我的问题是什么。我目前正在使用一个插件,我正在尝试使用数据库中的数据(生日)在我的日历中显示事件。我的php查询中的结果是预先格式化的 我的PHP是这样的: <?php require 'connection.php'; error_reporting(0); session_start(); $currentYear = date("Y"); $query = mysqli_query($con,"SELECT
<?php
require 'connection.php';
error_reporting(0);
session_start();
$currentYear = date("Y");
$query = mysqli_query($con,"SELECT * FROM table2");
$count = mysqli_num_rows($query);
if($count < 1){ }else{
while($row = mysqli_fetch_array($query)){
echo ' { date: \'' .$currentYear. '-' .$row['BirthMonth']. '-' .$row['BirthDay']. '\', title: \'' . $row['LastName'] . ' birthday \' }, ' ;
}
}
?>
我对CLNDR.js的jquery代码如下:
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'function/serverdays.php',
dataType: 'text',
success: function(data) {
var year = moment().year();
var newyear = moment(year + '-01-01').format('YYYY-MM-DD');
var valentine = moment(year + '-02-14').format('YYYY-MM-DD');
var allsaints = moment(year + '-11-01').format('YYYY-MM-DD');
var allsouls = moment(year + '-11-02').format('YYYY-MM-DD');
var christmas = moment(year + '-12-25').format('YYYY-MM-DD');
var lastday = moment(year + '-12-31').format('YYYY-MM-DD');
var events = [
data,
{ date: newyear, title: 'NEW YEAR'},
{ date: valentine, title: 'VALENTINE\'S DAY'},
{ date: allsaints, title: 'ALL SAINTS DAY'},
{ date: allsouls, title: 'ALL SOULS DAY'},
{ date: christmas, title: 'CHRISTMAS DAY'},
{ date: lastday, title: 'LAST DAY OF THE YEAR'},
];
$('#mini-clndr').clndr({
template: $('#calendar-template').html(),
events: events,
constraints: {
startDate: moment(year + '-01-01'),
endDate: moment(year + '-12-31')
},
clickEvents: {
click: function(target) {
if(target.events.length) {
var daysContainer = $('#mini-clndr').find('.days-container');
daysContainer.toggleClass('show-events', true);
$('#mini-clndr').find('.x-button').click( function() {
daysContainer.toggleClass('show-events', false);
});
}
}
},
adjacentDaysChangeMonth: true,
forceSixRows: true
});
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
});
</script>
我的html标签
<div id='mini-clndr'></div>
<script id="calendar-template" type="text/template">
<div class="controls">
<div class="clndr-previous-button">‹</div><div class="month"><%= month %></div><div class="clndr-next-button">›</div>
</div>
<div class="days-container">
<div class="days">
<div class="headers">
<% _.each(daysOfTheWeek, function(day) { %><div class="day-header"><%= day %></div><% }); %>
</div>
<% _.each(days, function(day) { %><div class="<%= day.classes %>" id="<%= day.id %>"><%= day.day %></div><% }); %>
</div>
<div class="events">
<div class="headers">
<div class="x-button">x</div>
<div class="event-header">EVENTS</div>
</div>
<div class="events-list">
<% _.each(eventsThisMonth, function(event) { %>
<div class="event">
<div class="event-item-name"><%= event.date %> : <%= event.title %></div>
<div class="event-item-location"><%= event.location %></div>
</div>
<% }); %>
</div>
</div>
</div>
</script>
&伊萨库&rsaquo;
x
事件
:
但是我从控制台得到一个指向插件的错误
未捕获的TypeError:无法读取未定义的属性“format”
不过,我对jquery不是很在行,而且我很难找到对“clndr.js”使用ajax的支持,因为关于它的帖子很少。可能的解决方案是什么?您没有返回有效的JSON。我强烈建议您使用PHP中需要的属性创建一个对象数组,然后使用
json_encode()
返回它。我同意@rorymcrossan的说法,PHP返回的字符串不是有效的json字符串。感谢您的快速响应,我在ajax中使用的数据类型如何?使用json_encode()返回结果后,是否还需要将“text”类型更改为“json”?那我现在就去试试。
var events = [
{ date: '2015-5-29', title: 'Williams birthday' },
{ date: '2015-5-29', title: 'Marcus birthday' },
{ date: '2015-1-19', title: 'Spear birthday' },
{ date: newyear, title: 'NEW YEAR'},
{ date: valentine, title: 'VALENTINE\'S DAY'},
{ date: allsaints, title: 'ALL SAINTS DAY'},
{ date: allsouls, title: 'ALL SOULS DAY'},
{ date: christmas, title: 'CHRISTMAS DAY'},
{ date: lastday, title: 'LAST DAY OF THE YEAR'},
];
<div id='mini-clndr'></div>
<script id="calendar-template" type="text/template">
<div class="controls">
<div class="clndr-previous-button">‹</div><div class="month"><%= month %></div><div class="clndr-next-button">›</div>
</div>
<div class="days-container">
<div class="days">
<div class="headers">
<% _.each(daysOfTheWeek, function(day) { %><div class="day-header"><%= day %></div><% }); %>
</div>
<% _.each(days, function(day) { %><div class="<%= day.classes %>" id="<%= day.id %>"><%= day.day %></div><% }); %>
</div>
<div class="events">
<div class="headers">
<div class="x-button">x</div>
<div class="event-header">EVENTS</div>
</div>
<div class="events-list">
<% _.each(eventsThisMonth, function(event) { %>
<div class="event">
<div class="event-item-name"><%= event.date %> : <%= event.title %></div>
<div class="event-item-location"><%= event.location %></div>
</div>
<% }); %>
</div>
</div>
</div>
</script>