我们使用AJAX来显示用户的统计信息 - 每月(当前月份和前12个月)发送和接收的邮件数量。我正在使用Google图表。当我没有使用AJAX的时候,它可以工作,但使用AJAX Google图表不起作用 - 整个页面变成空白,控制台中也没有例外。这里是我的代码:谷歌图表不能正常工作
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
$(document).ready(function() {
var params = {
"email": "{{ statistics_user_email }}",
"today_email_statistics_query_job_json": JSON.stringify({{ today_email_statistics_query_job_json|safe }}),
"this_month_email_statistics_query_job_json": JSON.stringify({{ this_month_email_statistics_query_job_json|safe }}),
"last_13_months_monthly_email_statistics_query_job_json": JSON.stringify({{ last_13_months_monthly_email_statistics_query_job_json|safe }})
};
function try_ajax() {
$.ajax({
type: 'POST',
url: '/ajax/user/email_statistics',
data: params,
success: function(reply) {
if (reply.wait === true) {
// Try again after 4 seconds.
setTimeout(try_ajax, 4000);
} else if (reply.ready === true) {
function drawTitleSubtitle() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Month');
data.addColumn('number', 'Emails sent');
data.addColumn('number', 'Emails received');
var rows = [];
for (var i = 0; i < reply["last_13_months_monthly_email_statistics"].length; i++) {
var row = reply["last_13_months_monthly_email_statistics"][i];
rows.push([row["month"], row["sent"], row["received"]]);
}
data.addRows(rows);
var options = {
'title': 'How Many Vegan Cheeseburgers I Ate Last Night',
'width': 400,
'height': 300
};
var material = new google.charts.Bar(document.getElementById('last-13-month-chart-div'));
material.draw(data, options);
}
$("#today-emails-sent-span").html(reply["today_email_statistics"]["messages_sent"]);
$("#today-emails-received-span").html(reply["today_email_statistics"]["messages_received"]);
$("#this-month-emails-sent-span").html(reply["this_month_email_statistics"]["messages_sent"]);
$("#this-month-emails-received-span").html(reply["this_month_email_statistics"]["messages_received"]);
google.load('visualization', '1', {packages: ['corechart', 'bar']});
google.setOnLoadCallback(drawTitleSubtitle);
$("#please-wait-div").addClass("hidden");
$("#email-statistics-div").removeClass("hidden");
} else {
console.error("AJAX returned unexpected results!", reply);
}
}
});
}
try_ajax();
});
</script>
当我评论以下行,它的工作原理没有图表:
google.load('visualization', '1', {packages: ['corechart', 'bar']});
google.setOnLoadCallback(drawTitleSubtitle);
的AJAX返回此对象一次:
{"wait": true}
了第二遍:
{"ready": true, "last_13_months_monthly_email_statistics": [{"received": 23, "sent": 2, "month": "2015-10"}, {"received": 15, "sent": 4, "month": "2015-11"}], "this_month_email_statistics": {"messages_received": 15, "messages_sent": 4}, "today_email_statistics": {"messages_received": 0, "messages_sent": 1}}
我是不包括HTML,但页面上存在所有元素。除了图表之外的所有东西都可以工作,在我没有使用AJAX的时候,这个工作方式很有用。问题是什么?
顺便说一句,我只加载了本月和上个月的数据,这不是一个错误。未来我们可以显示前12个月。
我试图在功能drawTitleSubtitle()
中使用console.log,但它似乎根本没有被调用。但在AJAX返回结果(第二次调用结果)后<html>
内部没有<body>
。
JS小提琴 - http://jsfiddle.net/uriwise/Lpsb0tqn/
也许谷歌不喜欢的素食干酪牛肉三明治这是:) –
@ T3H40比萨饼(从https://google-developers.appspot.com/chart复制/ interactive/docs/quick_start),我改变了它::-) – Uri
你走了!问题解决,修复和关闭。 –