我正在从PHP中的数据库中检索一组日期,并将其编码为JSON。我试图在我的javascript文件中检索json,以便日期可用于禁用datepicker UI中的日期。
当我在Firefox中运行项目并检查元素时,我收到此错误:
no element found checkDates.php:32:3
当我运行php文件时,输出是预期的,没有错误。
checkDates.php
<?php
//Set document mime type... this is important since you want to return json not text!
header('Content-Type: application/json');
$servername = "localhost";
$username = "user";
$password = "user";
$dbname = "ebooking";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select booking_date from booking";
$result = $conn->query($sql);
$checkDates = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$checkDates[] = $row['booking_date'];
}
} else {
echo "0 results";
}
echo json_encode($checkDates);
$conn->close();
?>
的index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
Date:
<input type="text" id="datepicker">
</body>
<script>
var badDates //= ["21-03-2016","31-03-2016","31-03-2016","30-03-2016"];
$.getJSON('checkDates.php', function(json){badDates=json,
console.dir(badDates);});
/*$.getJSON( "checkDates.php", function(json) {
console.log( "success" );
}).done(function() {
console.log( "second success" );
})
.fail(function() {
console.log( "error" );
})
.always(function() {
console.log( "complete" );
});*/
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: 'dd-mm-yy',
beforeShowDay: function(date) {
if($.inArray($.datepicker.formatDate('dd-mm-yy', date ), badDates) > -1)
{
return [false,"Unavailable","Booked"];
}
else
{
return [true,"Available","Not Booked"];
}
}
});
})
</script>
</html>