如果表是空的,如何处理json格式错误

时间:2017-02-01 15:15:55

标签: php jquery mysql zend-framework

从表中获取值,它的工作正常。当表空时我得到aler box json格式化错误。我该如何处理这个错误?

我的sql查询如下,我使用的是php zend framework

public function getProductsAction(){

     $oProductModel = new Application_Model_Db_Table_Products();
     $oSelect = $oProductModel->fetchAllProductItems();

     echo Zend_Json::encode($this->_helper->DataTables($oSelect, array('product_id','e.ename as employee_name','name','brand','conditions','about','image_path','reserved_price','Max(b.bid_amount) as amount')));

}

和查看页面

$(document).ready(function(){

    jQuery('#product-table').dataTable({
        <?php if(Qsm_User::isAdmin()){ ?>
            "sDom": 'Tlfrtip',
            "oTableTools": {
                "sSwfPath": "/js/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf",
                "aButtons": [{
                        "sExtends":    "collection",
                        "sButtonText": 'Save <span class="caret" />',
                        "aButtons":    [ "csv", "xls", "pdf" ]
                    }]
            },
       <?php } ?>
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "bServerSide": false,
        "sAjaxSource": '/buyproduct/api/get-products',

        "aoColumns": [  
            {"sTitle":"ID", "bVisible":false}, 
            {"sTitle":"Emp Name", "bVisible":true},
            {"sTitle":"Product", "sWidth": '10%'},
            {"sTitle":"Brand", "sWidth": '10%'},
            {"sTitle":"Condition", "sWidth": '10%'},
            {"sTitle":"Description", "sWidth": '20%'},
            {"sTitle":"Image","sWidth": '18%',
             "mData": null,
             "bSearchable": false, 
             "bSortable": false,
             "fnRender": function (oObj){
            var actions = "";
            actions += "<span class=''>\n\
             <img src='/uploads/gallery/" + oObj.aData[6] + "' alt='Image' title='Image' class='style_prevu_kit' width='0px' height='0px'  />";
                actions +="</span>";
            return actions;
            }
            },
            {"sTitle":"Starting Bid Price", "sWidth": '10%'},
            {"sTitle":"Current Bid Price", "sWidth": '10%'},
            {
                "sTitle":"Bid",
                "sWidth": '17%',
                "mData": null,
                "bSearchable": false,                               
                "bSortable": false,
                "fnRender": function (oObj){
                    var actions = "";
                    actions += "<span class='data_actions iconsweet'>\n\
                               <a title='Buy' href='/buyproduct/index/bid/id/" + oObj.aData[0] + "'><img src='/images/buy.png' alt='Buy'  title='Buy' /></a>";
                    actions +="</span>";
                    return actions;
                 }
            }
            <?php if(Qsm_User::isAdmin()){ ?>
            ,{
                "sTitle":"Transaction",
                "sWidth": '22%',
                "mData": null,
                "bSearchable": false,
                "bSortable": false,
                "fnRender": function (oObj){
                    var actions1 = "";
                    actions1 += "<span class='data_actions iconsweet'>\n\
                                <a title='Transaction' href='/buyproduct/index/transaction/id/" + oObj.aData[0] + "'><img src='/images/icons/edit3.png' alt='Edit' title='Transaction' /></a>";

                    actions1 +="</span>";
                    return actions1;
                }
            }
            <?php } ?>
        ]
    });     

});

请有人帮我处理这个json格式化错误

1 个答案:

答案 0 :(得分:0)

你的动作将在无行条件下返回空字符串,这是无效的json数据,检查从

返回的值
$this->_helper->DataTables($oSelect, array('product_id','e.ename as employee_name','name','brand','conditions','about','image_path','reserved_price','Max(b.bid_amount) as amount'))

如果null /或没有行回显像这样的行或空行数据

{
   "msg": "no-data"
}

更新

public function getProductsAction(){

 $oProductModel = new Application_Model_Db_Table_Products();
 $oSelect = $oProductModel->fetchAllProductItems();
 if($oSelect){
    echo Zend_Json::encode($this->_helper->DataTables($oSelect, array('product_id','e.ename as employee_name','name','brand','conditions','about','image_path','reserved_price','Max(b.bid_amount) as amount')));
 }else{
    echo '{"msg": "no-data"}';  //or edit with empty data matching required by your view
 }
}