尝试使用json作为响应将mysql数据库数据填充到html模板时出错

时间:2018-08-06 02:42:15

标签: php arrays json html5

  getUserInvoicesList: function(){
    $$('.page-content').hide();
    var sessionData = JSON.parse(localStorage.getItem('session'));
    //formate the date object as a string M/D/YYYY (american)
    function formatDate(date){
      var date = new Date(date);
      return date.getMonth()+1+'/'+date.getDate()+'/'+date.getFullYear();
    }
    //returns a string with populated data attributes
    function template(data){
      return '<li class="swipeout">'+
      '<a href="/invoice-view/'+data[0]+'" class="item-link item-content swipeout-content">'+
      '<div class="item-inner">'+
        '<div class="item-title-row">'+
          '<div class="item-title">#'+data[0]+'</div>'+
          '<div class="item-after inv-amount">$'+data[6]+'</div>'+
        '</div>'+
        '<div class="item-subtitle m-t-5">'+
         '<span class="inv-client">Due date: <strong>'+formatDate(data[4])+'</strong> </span>'+
         '<!--<span class="badge color-green inv-status">Paid*/</span>-->'+
         '</div>'+
      '</div>'+
    '</a>'+
    '<div class="swipeout-actions-right">'+
      '<a href="/edit-invoice/" class="color-green">Edit</a>'+
      '<a href="#" class="color-red swipeout-delete">Delete</a>'+
    '</div>'+
    '</li>'
    }

    app.request.post(global.url, JSON.stringify({id: sessionData.id, action: 'getuserinvoiceslist'}), function(response){
      var responseData = JSON.parse(response);
      //query the ul #invoice-list that contain the invoices <li> elements
      var $invoiceList = $$('#invoice-list');
      var user = responseData.data;
      //conviniently assing the data to invoices variable
      var invoices = responseData.data;
      //map through all invoices and reutrn the strings for the html
      //by calling the template function with the data for each invoice
      //and join the end result as a string
      var invoicesHtml = invoices.map(function(invoice,item){
          return(template(invoice))
      }).join('');
      //finaly switch the old HTML content of the ul with the generated html string 
      $invoiceList.html(invoicesHtml)
      $$('.emitido').text(user.emitido);
      $$('.vencimiento').text(user.vencimiento);
      $$('.estado').text(user.estado);
      $$('.estado').text(user.estado);
      $$('.subtotal').text(user.subtotal);
      $$('.iva_igv').text(user.iva_igv);
      $$('.id').text(user.id);
      $$('.page-content').show();
    }, function(error){

    })
  }, 

它基本上是将一些数据填充到html模板(列表项)

像附件图片: enter image description here

现在有时有效,有时无效。当一切正常后,我将收到以下响应:

{"status":200,"data":[["00000431","0","000010","2018-01-01","2018-01-30",null,"0.37","Contado","No pagado","1","0.00","0",null,"0.02","0.35","SI","----","----","0","0","NO","","0.00","","","","0.00","","","0.00","",""],["00000432","431","000010","2018-01-01","2018-01-30",null,"1.39","Contado","No pagado","2","0.00","0",null,"0.09","1.30","SI","----","----","0","0","NO","","0.00","","","","0.00","","","0.00","",""]]}

但不是的时候,我会在Google chrome控制台中收到以下消息:

VM2618:1 Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Object.success (ajaxcall.js:81)
    at r (framework7.min.js:12)
    at XMLHttpRequest.Request.f.onload (framework7.min.js:12)

就像json响应为空。但是有时它是如何工作的?你能帮我吗?

谢谢。

0 个答案:

没有答案