2017-02-11 21 views
1

請幫助我。我遇到了一個DataTable警告,如「DataTables警告:服務器的JSON數據無法解析,這是由JSON格式錯誤導致的。」在使用PHP的Zend框架中,使用JSON編碼。無法解析來自服務器的JSON數據。這是由Zend框架中的JSON格式錯誤引起的,同時使用

這個警告只發生在表爲空時,但是這個問題只是在sql查詢中使用group關鍵字時纔會出現,如果我不使用group關鍵字,那麼它只給出表中的一條記錄,但表中有更多記錄也。當我使用以下查詢時,輸出變爲顯示所有記錄,只有表中有數據,如果不顯示數據表示警告。 // sql查詢(型號/表/ product.php)

public function fetchAllProductItems() { 
$oSelect = $this->select() 
       ->setIntegrityCheck(false) 
       ->from(array("p" => "products","b" => "bid"), ('*')) 
       ->joinLeft(array("b" => "bid"), "b.product_id=p.product_id", array('bid_id','bid_amount')) 
       ->joinInner(array("e" => "employees"), "e.employee_id=p.employee_id",array('ename')) 
       ->where("p.verified = ?", "Yes") 
       ->where("p.sold_out = ?", "No") 
       ->group('p.product_id') 
       ->having("p.sale_end_date >= ?", date("Y-m-d")); 
     return $oSelect; 
    } 

// JSON編碼(模塊/銷售/控制器/ apicontroller)

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'))); 

} 

下面的查詢將只顯示一個記錄,如果表中有多個記錄。如果表格是空的,那麼我會來「表格消息中沒有數據可用」。

// sql查詢(型號/表/ product.php)

$oSelect = $this->select() 
       ->setIntegrityCheck(false) 
       ->from(array("p" => "products","b" => "bid"), ('*')) 
       ->joinLeft(array("b" => "bid"), "b.product_id=p.product_id", array('bid_id','bid_amount')) 
       ->joinInner(array("e" => "employees"), "e.employee_id=p.employee_id",array('ename')) 
       ->where("p.verified = ?", "Yes") 
       ->where("p.sold_out = ?", "No") 

       ->where("p.sale_end_date >= ?", date("Y-m-d")); 

回答

0

正確的方法是將「ViewJsonStrategy」添加到您的模塊的配置,並將您的視圖模型爲「終端」。

類似的東西:

public function getProductsAction() 
{ 
    $oProductModel = new Application_Model_Db_Table_Products(); 
    $oSelect = $oProductModel->fetchAllProductItems(); 
    //ViewModel requires one array 
    $data = ['result'=>$this->_helper->DataTables($oSelect, ...)]; 
    $view = new ViewModel($data)->setTerminal(true); 
} 

我已經在GitHub的一個例子,可能有助於:

https://github.com/fmacias/SMSDispatcher

鏈接到官方文檔: https://framework.zend.com/manual/2.4/en/modules/zend.view.quick-start.html

另一方面,如果您想將對象反序列化爲JSON並回顯此輸出,則應該先設置標題。

隨着純PHP:

header('Content-type: application/json;charset=UTF-8'); 
echo json_encode($someData); 

與Zend這樣的事情:

$this->getResponse()->setHeader('ContentType','application/json;charset=utf-8'); 
echo $yourJson; 
相關問題