好吧,我正在执行AJAX请求,以从运行Flask应用程序的本地服务器中的MySql数据库获取JSON数据。我知道我所有的背景资料都在工作,因为当我尝试访问为我提供所有JSON数据的URL时,它可以工作,并且可以在其中看到我的所有JSON。
数据长度约为2000项
这是1个项目的度量:
{
“ col1”:“ 2014-02-18”,
“ col2”:“ {\”数据\“:{\”消息类型\“:{\” txt4 \“:{\” TotalF \“:0,\” TotalS \“:1},\”总计\“ :{\“失败\”:2,\“成功\”:7},\“ txt1 \”:{\“ TotalF \”:0,\“ TotalS \”:2},\“ txt2 \”:{ \“ TotalF \”:0,\“ TotalS \”:2},\“ txt3 \”:{\“ TotalF \”:2,\“ TotalS \”:2}}}}“,
“ col3”:“ {\” 717 \“:{\” Log \“:{\” msg_medium \“:\” txt3 \“,\” msg_content \“:\”使用Climote。存钱。立即登录!\“,\” state \“:\”成功\“,\” message_sent \“:1,\” running_state \“:\” Success \“}}},\” 1469 \“:{\” Log \“:{\” msg_medium \“:\” txt1 \“,\” msg_content \“:\”您知道与Climote交往会降低您的取暖费吗?\“,\” state \“:\”成功\“ ,\“ message_sent \”:0,\“ running_state \”:\“成功\”}},\“ 3736 \”:{\“ Log \”:{\“ msg_medium \”:\“ txt3 \”,\ “ msg_content \”:\“使用Climote。存钱。立即登录!\“,\” state \“:\”成功\“,\” message_sent \“:0,\” running_state \“:\”成功\“}}},\” 102 \“:{\”登录\“:{\” msg_medium \“:\” txt3 \“,\” msg_content \“:\”使用Climote。存钱。立即登录!\“,\” state \“:\”成功\“,\” message_sent \“:0,\” running_state \“:\”成功\“}}},\” 297 \“:{\”登录\“:{\” msg_medium \“:\” txt3 \“,\” msg_content \“:\”使用Climote。存钱。立即登录!\“,\” state \“:\”成功\“,\” message_sent \“:1,\” running_state \“:\”成功\“}}},\” 139 \“:{\”登录\“:{\” msg_medium \“:\” txt2 \“,\” msg_content \“:\”您是否知道3个邻居使用的Climote更少或与您相同,0的邻居使用的是Climote?\“,\”状态\ “:\”成功\“,\”消息发送\“:0,\”运行状态\“:\”成功\“}},\” 398 \“:{\”日志\“:{\” msg_medium \“: \“ txt3 \”,\“ msg_content \”:\“使用Climote。存钱。立即登录!\“,\” state \“:\”故障\“,\”消息发送\“:1,\”运行状态\“:\”故障\“}}},\” 85 \“:{\”登录\“:{\” msg_medium \“:\” txt3 \“,\” msg_content \“:\”使用Climote。存钱。立即登录!\“,\” state \“:\”成功\“,\”消息发送\“:1,\”运行状态\“:\”成功\“}}},\” 291 \“:{\”登录\“:{\” msg_medium \“:\” txt2 \“,\” msg_content \“:\”您知道2个邻居使用或更少使用Climote吗?1个邻居使用了吗? “:\”成功\“,\”消息发送\“:0,\”运行状态\“:\”成功\“}}}”,
“ col4”:“ {\” txt4 \“:0,\” txt1 \“:2,2,\” txt2 \“:2,2,\” txt3 \“:5}”
}
我一遍又一遍地提出请求,以某种方式返回的数据正在增加,我相信这是由于浏览器的缓存...第一次返回33 itens,然后是66、99 ...现在是495 = /
在Chrome浏览器的开发人员工具中,我收到了一个错误:
“无法加载资源:服务器响应状态为409(冲突)”
这是我的代码:
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<!--Load JQuery-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
google.load('visualization', '1.0', {'packages':['corechart']});
$(document).ready(function () {
$.getJSON( "http://127.0.0.1:53000/get_json", function(data) {
var list_results = [];
list_results = data.results
console.log(list_results.length);
console.log(data);
myLiteralObj = {
cols: [],
rows: []
};
myLiteralObj.cols.push({id: 'Date', label: 'Date', type: 'date'},
{id: 'SuccessMsgs', label: 'SuccessMsgs', type: 'number'},
{id: 'FailureMsgs', label: 'FailureMsgs', type: 'number'},
{id: 'SuccessMsg1', label: 'SuccessMsg1', type: 'number'},
{id: 'FailureMsg1', label: 'FailureMsg1', type: 'number'},
{id: 'SuccessMsg2', label: 'SuccessMsg2', type: 'number'},
{id: 'FailureMsg2', label: 'FailureMsg2', type: 'number'},
{id: 'SuccessMsg3', label: 'SuccessMsg3', type: 'number'},
{id: 'FailureMsg3', label: 'FailureMsg3', type: 'number'},
{id: 'SuccessMsg4', label: 'SuccessMsg4', type: 'number'},
{id: 'FailureMsg4', label: 'FailureMsg4', type: 'number'}
);
for (x=0; x<list_results.length; x++) {
var item = $.parseJSON(list_results[x].col2);
var arrayDate = list_results[x].col1.split("-");
var newDate = new Date(arrayDate[0],arrayDate[1]-1,arrayDate[2]);
myLiteralObj.rows.push({c:[
{v: newDate},
{v: item['Data']['Message Types']['Totals']['Success']},
{v: item['Data']['Message Types']['Totals']['Failure']},
{v: item['Data']['Message Types']['txt1']['TotalS']},
{v: item['Data']['Message Types']['txt1']['TotalF']},
{v: item['Data']['Message Types']['txt2']['TotalS']},
{v: item['Data']['Message Types']['txt2']['TotalF']},
{v: item['Data']['Message Types']['txt3']['TotalS']},
{v: item['Data']['Message Types']['txt3']['TotalF']},
{v: item['Data']['Message Types']['txt4']['TotalS']},
{v: item['Data']['Message Types']['txt4']['TotalF']}
]});
}
//Getting the data into the dataTable
var dataTotal = new google.visualization.DataTable(myLiteralObj);
console.log(dataTotal);
//Creating first view total msgs
var viewTotal = new google.visualization.DataView(dataTotal);
viewTotal.setColumns([0,1,2,{calc:totalMessagesTotal, type:'number', label:'Total Messages'}]);
console.log(viewTotal);
function totalMessagesTotal(dataTable, rowNum) {
return dataTable.getValue(rowNum,1) + dataTable.getValue(rowNum,2);
}
var optionViewTotal = {
vAxis: {title: "Number of Messages"},
hAxis: {title: "Date"},
seriesType: "bars",
series: {2: {type: "line"}},
width: 1000,
height: 240
};
var combo = new google.visualization.ComboChart($('#chart2').children('div')[0]);
combo.draw(viewTotal, optionViewTotal);
//Creating view msg 1
var view1 = new google.visualization.DataView(dataTotal);
view1.setColumns([0,3,4,{calc:totalMessages1, type:'number', label:'Total Messages'}]);
console.log(view1);
function totalMessages1(dataTable, rowNum) {
return dataTable.getValue(rowNum,3) + dataTable.getValue(rowNum,4);
}
var combo1 = new google.visualization.ComboChart($('#chart_msg1').children('div')[0]);
combo1.draw(view1, optionViewTotal);
//Creating view msg 2
var view2 = new google.visualization.DataView(dataTotal);
view2.setColumns([0,5,6,{calc:totalMessages2, type:'number', label:'Total Messages'}]);
console.log(view2);
function totalMessages2(dataTable, rowNum) {
return dataTable.getValue(rowNum,5) + dataTable.getValue(rowNum,6);
}
var combo2 = new google.visualization.ComboChart($('#chart_msg2').children('div')[0]);
combo2.draw(view2, optionViewTotal);
//Creating view msg 3
var view3 = new google.visualization.DataView(dataTotal);
view3.setColumns([0,7,8,{calc:totalMessages3, type:'number', label:'Total Messages'}]);
console.log(view3);
function totalMessages3(dataTable, rowNum) {
return dataTable.getValue(rowNum,7) + dataTable.getValue(rowNum,8);
}
var combo3 = new google.visualization.ComboChart($('#chart_msg3').children('div')[0]);
combo3.draw(view3, optionViewTotal);
//Creating view msg 4
var view4 = new google.visualization.DataView(dataTotal);
view4.setColumns([0,9,10,{calc:totalMessages4, type:'number', label:'Total Messages'}]);
console.log(view4);
function totalMessages4(dataTable, rowNum) {
return dataTable.getValue(rowNum,9) + dataTable.getValue(rowNum,10);
}
var combo4 = new google.visualization.ComboChart($('#chart_msg4').children('div')[0]);
combo4.draw(view4, optionViewTotal);
var data2 = new google.visualization.DataTable();
data2.addColumn('string', 'Topping');
data2.addColumn('number', 'Slices');
data2.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':500,
'height':300};
var chart2 = new google.visualization.PieChart($('#chart1').children('div')[0]);
chart2.draw(data2, options);
//All h2 elements when clicked will toggle the next div element near them
$('h2').click(function () {
$(this).next('div').toggle();
});
});
});
</script>
</head>
<body>
<div id="container">
<div id="chart1">
<h2>Behaving Customers</h2>
<div class="charts"></div>
</div>
<div id="chart2">
<h2>Overall Messages</h2>
<div class="charts"></div>
</div>
<div id="chart_msg1">
<h2>Message1</h2>
<div class="charts"></div>
</div>
<div id="chart_msg2">
<h2>Message2</h2>
<div class="charts"></div>
</div>
<div id="chart_msg3">
<h2>Message3</h2>
<div class="charts"></div>
</div>
<div id="chart_msg4">
<h2>Message4</h2>
<div class="charts"></div>
</div>
</div>
</body>
</html>
我认为这是与JSON大小有关的问题,或者是我的PC,浏览器等的某些限制...
我在Mac OS X-版本10.9.4-4GB内存上运行,并且在Google Chrome上是最新的。
问候-如果需要,我的服务器端代码在这里:
app = Flask(__name__)
db_owen = MySQLdb.connect("localhost","root","","climote_trial")
results = []
@app.route('/get_json')
@crossdomain(origin='*')
def get_json():
c = db_owen.cursor()
c.execute("select * from trialresults")
jsonresult = c.fetchall()
for x in jsonresult:
dic = {'col1':str(x[0]),'col2':x[1],'col3':x[2],'col4':x[3]}
results.append(dic)
return jsonify({ 'results':results });
@app.route('/')
def index():
return render_template('number_1.html')
if __name__ == '__main__':
app.run(debug= True, port=53000)
最佳答案
我只了解python,但据我所知,您的问题出在您的服务器实现上。您定义一个全局数组results = []
,并在每次调用get_json()
时追加每个记录。但是您永远不会从Arary中删除元素。我想那不是你想做的。
关于javascript - Ajax请求未返回所有JSON-无法加载资源:服务器响应状态为409(冲突),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24617391/