Ajax请求不会返回所有JSON - 无法加载资源:服务器响应状态为409(冲突)

时间:2014-07-07 18:33:16

标签: javascript jquery python ajax json

好吧,我正在做一个AJAX请求,要从运行Flask应用程序的本地服务器的MySql数据库中获取JSON数据。我知道我的所有后台工作都有效,因为当我尝试访问提供我所有JSON数据的URL时,它可以工作,我可以看到我所有的JSON。

数据长度约为2000项

这是1项的衡量标准:

  

{         “col1”:“2014-02-18”,         “col2”:“{\”数据\“:{\”消息类型\“:{\”txt4 \“:{\”TotalF \“:0,\”TotalS \“:1},\”Totals \“ :{\“Failure \”:2,\“Success \”: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。存钱。今天登录!\“,\”状态\“:\”成功\“,\”message_sent \“:1,\”running_state \“:\”成功\“}},\”1469 \“:{\”日志\“:{\”msg_medium \“:\”txt1 \“,\”msg_content \“:\”你知道与Climote合作可以降低你的取暖费吗?“,”状态“:”成功“ ,“message_sent \”:0,\“running_state \”:\“Success \”}},\“3736 \”:{\“Log \”:{\“msg_medium \”:\“txt3 \”,\ “msg_content \”:\“使用Climote。存钱。今天登录!\“,\”状态\“:\”成功\“,\”message_sent \“:0,\”running_state \“:\”成功\“}},\”102 \“:{\”日志\“:{\”msg_medium \“:\”txt3 \“,\”msg_content \“:\”使用Climote。存钱。今天登录!\“,\”状态\“:\”成功\“,\”message_sent \“:0,\”running_state \“:\”成功\“}},\”297 \“:{\”日志\“:{\”msg_medium \“:\”txt3 \“,\”msg_content \“:\”使用Climote。存钱。今天登录!\“,\”状态\“:\”成功\“,\”message_sent \“:1,\”running_state \“:\”成功\“}},\”139 \“:{\”日志\“:{\”msg_medium \“:\”txt2 \“,\”msg_content \“:\”你知道3个邻居使用的Climote与你相比更少或更少,0更多地使用它吗?“,”状态\ “:”成功\“,\”message_sent \“:0,\”running_state \“:\”成功\“}},\”398 \“:{\”Log \“:{\”msg_medium \“: \“txt3 \”,\“msg_content \”:\“使用Climote。存钱。今天登录!\“,\”state \“:\”Failure \“,\”message_sent \“:1,\”running_state \“:\”Failure \“}},\”85 \“:{\”Log \“:{\”msg_medium \“:\”txt3 \“,\”msg_content \“:\”使用Climote。存钱。今天登录!\“,\”状态\“:\”成功\“,\”message_sent \“:1,\”running_state \“:\”成功\“}},\”291 \“:{\”日志\“:{\”msg_medium \“:\”txt2 \“,\”msg_content \“:\”你知道2个邻居使用Climote的次数与你相同,1使用它更多吗?\“,”状态\ “:\”成功\“,\”message_sent \“:0,\”running_state \“:\”成功\“}}}”,         “col4”:“{\”txt4 \“:0,\”txt1 \“:2,\”txt2 \“: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)

1 个答案:

答案 0 :(得分:0)

我只知道python一点点,但据我所知,你的问题在于你的服务器实现。您定义全局数组results = [],并在每次调用get_json()时附加每条记录。但是你永远不会从文学中删除一个元素。我想这不是你想要做的。