我试图让.js文件填充MySQL数据库中的内容。
我有一个.php文件可以完成所有MySQL的工作:
$result = mysql_query("SELECT *,
DATE_FORMAT(gig_dateTime, '%Y') as gigYear,
DATE_FORMAT(gig_dateTime, '%m') as gigMonth,
DATE_FORMAT(gig_dateTime, '%d') as gigDay,
DATE_FORMAT(gig_dateTime, '%H') as gigHour,
DATE_FORMAT(gig_dateTime, '%i') as gigMinute
FROM live
WHERE gig_dateTime > NOW()
ORDER BY gig_dateTime DESC");
$to_encode = array();
while($row = mysql_fetch_assoc($result)) {
$to_encode[] = $row;
}
echo json_encode($to_encode);
然后在.js文件中我有以下内容:
$.getJSON('live-dates.php', function(data) {
var myConcerts = [];
$.each(data, function(index, d){
myConcerts.push(
"{",
"year:", d.gigYear, ",",
"month:", d.gigYear, ",",
"day:", d.gigYear, ",",
"hour:", d,gigHour, ",",
"minute:", d.gigMinute, ",",
"latitude:", d.gig_lat, ",",
"longitude:", d.gig_long, ",",
"location:", d.gig_location, ",",
"infoWindow:", d.gig_info, "",
"}"
);
});
});
但在加载页面时,我得到未捕获的ReferenceError:$未定义
在显示.js文件的index.php文件中,我包含以下内容:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/myconcerts.js" type="text/javascript"></script>
我正在尝试做什么,或者我是否采取了错误的方式?
由于
答案 0 :(得分:-1)
也许你需要用$.getJSON(...)
包裹$(function(){ /* $.getJSON() HERE */ });
,这与$(document).ready(...);
相同。