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

Ajax request don't return all the JSON - Failed to load resource: the server responded with a status of 409 (Conflict)

本文关键字:状态 响应 服务器 冲突 失败 资源 返回 请求 加载 JSON Ajax      更新时间:2023-09-26

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

数据的长度约为2000项

这是1项的度量:

{"col1":"2014-02-18",:"col2{'"数据'":{'"消息类型'":{'"txt4 '":{'"TotalF '":0,'"总额'":1},'"总额'":{'"失败'":2,'"成功'":7},'"txt1 '":{'"TotalF '":0,'"总额'":2},'"txt2 '":{'"TotalF '":0,'"总额'":2},'"txt3 '":{'"TotalF '":2,'"总额'":2}}}}",:"col3{'"717 '":{'"日志'":{'"msg_medium '",'"txt3 '",'"msg_content '",'"Climote使用。省钱。今天登录!' '",'"状态":'"成功'",'"message_sent '":1,'"running_state ' ' ": ' "成功"}},'"1469 '":{'"日志'":{'"msg_medium '",'"txt1 '",'"msg_content '",'"你知道与Climote可以降低你的取暖费用吗?' '",'"状态":'"成功'",'"message_sent '":0,'"running_state ' ' ": ' "成功"}},'"3736 '":{'"日志'":{'"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使用。省钱。今天登录!'"state'": '"Success'", '"message_sent'": 1, '"running_state'": '"Success'"}}, '"139'": {'"Log'": {'"msg_medium'": '"txt2'", '"msg_content'": '"你知道有3个邻居使用Climote的次数和你一样少还是一样多?' '",'"状态":'"成功'",'"message_sent '":0,'"running_state ' ' ": ' "成功"}},'"398 '":{'"日志'":{'"msg_medium '",'"txt3 '",'"msg_content '",'"Climote使用。省钱。今天登录!' '",'"状态":'"失败'",'"message_sent '":1,'"running_state '":'"失败' "}},'"85 '":{'"日志'":{'"msg_medium '",'"txt3 '",'"msg_content '",'"Climote使用。省钱。今天登录!'"state'": '"Success'", '"message_sent'": 1, '"running_state'": '"Success'"}}, '"291'": {'"Log'": {'"msg_medium'": '"txt2'", '"msg_content'": '"你知道有两个邻居和你一样少用Climote,而我用得更多吗?' '",'"状态":'"成功'",'"message_sent '":0,'"running_state '":'"成功'"}}}:"col4{'"txt4 '":0,'"txt1 '":2,'"txt2 '":2,'"txt3 '":5}"}

我一遍又一遍地发出请求,不知何故返回的数据正在增加,我相信这是因为浏览器的缓存…第一次退了33件,后来是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上更新。

Regards -如果需要,我的服务器端代码在这里:

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()时附加每个记录。但是你永远不会从数组中删除一个元素。我猜那不是你想做的。