JSON data format error - result contains html view data rather json data

JSON data format error - result contains html view data rather json data

dineshdinesh Posts: 5Questions: 0Answers: 0
edited December 2010 in General
Hi,

Need your help, Every time I render my page I get Json data format error popup on all the browsers. Here is my code

Index.phtml


Users
IPAddresses






User Id
User Name
Active
Enable
Password Expired














table.js

$(document).ready(function() {
/* Build the DataTable with third column using our custom sort functions */
oTable= $('table#example').dataTable( {
"bProcessing": true,
"bJQueryUI": true,
"bServerSide": true,
"sAjaxSource": "admin/index/users",
"sPaginationType": "full_numbers"
} );
} );


After using the firbug I found that in the json result it contains the html view page rather json data hence parsing error.

Replies

  • franquisfranquis Posts: 7Questions: 0Answers: 0
    Hi,
    Can you post the content of your sAjaxSource ( admin/index/users )?
  • dineshdinesh Posts: 5Questions: 0Answers: 0
    Here it is
    [code]
    class Admin_UserController extends Zend_Controller_Action {

    /**
    * User Action
    * @return Json data
    */
    public function usersAction() {
    $config = Zend_Registry::get('config');

    $gaSql['user'] = $config->db->username;
    $gaSql['password'] = $config->db->password;
    $gaSql['db'] = $config->db->dbname;
    $gaSql['server'] = $config->db->host;
    $gaSql['type'] = "mysql";


    $gaSql['link'] = mysql_pconnect($gaSql['server'], $gaSql['user'], $gaSql['password']) or
    die('Could not open connection to server');

    mysql_select_db($gaSql['db'], $gaSql['link']) or
    die('Could not select database ' . $gaSql['db']);

    /* Paging */
    $sLimit = "";
    if (isset($_GET['iDisplayStart'])) {
    $sLimit = "LIMIT " . mysql_real_escape_string($_GET['iDisplayStart']) . ", " .
    mysql_real_escape_string($_GET['iDisplayLength']);
    }

    /* Ordering */
    $sOrder = "";
    if (isset($_GET['iSortCol_0'])) {
    $sOrder = "ORDER BY ";
    for ($i = 0; $i < mysql_real_escape_string($_GET['iSortingCols']); $i++) {
    $sOrder .= $this->fnColumnToField(mysql_real_escape_string($_GET['iSortCol_' . $i])) . "
    " . mysql_real_escape_string($_GET['sSortDir_' . $i]) . ", ";
    }
    $sOrder = substr_replace($sOrder, "", -2);
    }

    /* Filtering - NOTE this does not match the built-in DataTables filtering which does it
    * word by word on any field. It's possible to do here, but concerned about efficiency
    * on very large tables, and MySQL's regex functionality is very limited
    */
    $sWhere = "";
    if (isset($_GET['sSearch']) && $_GET['sSearch'] != "") {
    $sWhere = "WHERE UserId LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%' OR " .
    "UserName LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%' OR " .
    "Active LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%' OR " .
    "Enable LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%' OR " .
    "PasswordExpired LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%'";
    }

    $sQuery = "SELECT SQL_CALC_FOUND_ROWS UserId, UserName, Active, Enable, PasswordExpired
    FROM users $sWhere $sOrder $sLimit ";

    $rResult = mysql_query($sQuery, $gaSql['link']) or die(mysql_error());

    $sQuery = "SELECT FOUND_ROWS()";

    $rResultFilterTotal = mysql_query($sQuery, $gaSql['link']) or die(mysql_error());
    $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
    $iFilteredTotal = $aResultFilterTotal[0];

    $sQuery = "SELECT COUNT(UserId) FROM users";

    $rResultTotal = mysql_query($sQuery, $gaSql['link']) or die(mysql_error());
    $aResultTotal = mysql_fetch_array($rResultTotal);
    $iTotal = $aResultTotal[0];

    $sOutput = '{';
    if (isset($_GET['sEcho']))
    $sOutput .= '"sEcho": ' . intval($_GET['sEcho']) . ', ';
    $sOutput .= '"iTotalRecords": ' . $iTotal . ', ';
    $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', ';
    $sOutput .= '"aaData": [ ';
    while ($aRow = mysql_fetch_array($rResult)) {
    $sOutput .= "[";
    $sOutput .= '"' . addslashes($aRow['UserId']) . '",';
    $sOutput .= '"' . addslashes($aRow['UserName']) . '",';
    $sOutput .= '"' . addslashes($aRow['Active']) . '",';
    $sOutput .= '"' . addslashes($aRow['Enable']) . '",';
    $sOutput .= '"' . addslashes($aRow['PasswordExpired']) . '"';
    $sOutput .= "],";
    }
    $sOutput = substr_replace($sOutput, "", -1);
    $sOutput .= '] }';

    return $sOutput;
    }

    function fnColumnToField($i) {
    if ($i == 0)
    return "UserId";
    else if ($i == 1)
    return "UserName";
    else if ($i == 2)
    return "Active";
    else if ($i == 3)
    return "Enable";
    else if ($i == 4)
    return "PasswordExpired";
    }

    }
    [/code]
  • dineshdinesh Posts: 5Questions: 0Answers: 0
    This class name is incorrect (actually I have two different classes so I put wrong one here ) actually it is

    [code]
    class Admin_IndexController extends Zend_Controller_Action {
    [/code]

    So please ignore the name i.e. class Admin_UserController, rest of the code is exactly same.
  • franquisfranquis Posts: 7Questions: 0Answers: 0
    Maybe you can try the server-side code available here witch use the json_encode PHP function instead..

    http://www.datatables.net/development/server-side/php_mysql
  • dineshdinesh Posts: 5Questions: 0Answers: 0
    edited December 2010
    See this is what I get in both the cases

    [code]





















    Secure Documents



    Login



    Support



    <!-- Search form -->









    Call: 9899032002 toll free




    Welcome Administrator


    #container{margin-left: 10px;}



    Users
    IPAddresses
    Profiles






    User Id
    User Name
    Active
    Enable
    Password Expired






    User Id
    User Name
    Active
    Enable
    Password Expired












    {"sEcho": 1, "iTotalRecords": 16, "iTotalDisplayRecords": 16, "aaData": [ ["admin","Administrator","1","1","0"],["din","Dinesh Kumar","1","1","0"],["eight","Eight User","1","1","0"],["eleventh","Seventh User","1","1","0"],["fifth","Fifth User","1","1","0"],["first","First User","1","1","0"],["fourt","Fourth User","1","1","0"],["fourteen","Eight User","1","0","1"],["ninth","Fifth User","1","1","0"],["second","Second User","1","1","0"]] }


    Copyright © 2010



    [/data]
  • dineshdinesh Posts: 5Questions: 0Answers: 0
    But when I see your server side example output in the firebut I just see the json output not the html view page.

    Where exactly I am making the mistake ?
This discussion has been closed.