我收到了与JSON

时间:2016-01-07 08:12:00

标签: php jquery json web-deployment

我遇到一个错误,即'JSON无法解析'。这是由JSON格式错误引起的。

function aj() {
    $('#accounting').dataTable({
        'bProcessing': true,
        'bServerSide': true,
        "bSort": true,
        'bDestroy': true,
        "bAutoWidth": false,
        'sPaginationType': "full_numbers",
        'iDisplayLength': 25,
        'sAjaxSource': 'admin/datatables/accounting/ledger.php?userID=' + $('#users').val(),
        'aoColumns': [
            { 'mData': 'count' },
            { 'mData': 'AccountID' },
            { 'mData': 'Debit' },
            { 'mData': 'Credit' },
            { 'mData': 'CurrencyID' },
            { 'mData': 'Date' },
            { 'mData': 'Terms' },
            { "mData": 'Actions' }
        ]
    });
}
<select onchange="aj();" id="users">
    <option value=""></option>
    <?php
        $Acc = new BLL\Accounts;
        $Acc->get($arrayOfObjects);
        if (!empty($arrayOfObjects)) {
            foreach ($arrayOfObjects as $value) {
                echo "<option value=" . $value->getID() . "> " . $value->getAccountName() . "</option>";
            }
        } 
    ?>
</select>

ledger.php是

<?php
require_once '../../../layers/loader.php';
require_once '../../../includes/config.php';
require_once '../../../includes/session.php';
if ((isset($_SESSION['sess_staffid'])and $_SESSION['sess_staffid']!="" )) {
  $StaffID = $_SESSION['sess_staffid'];
} else {
    header("Location:" . SITE_URL . "/404.html");
    exit;
}
global $TablePrefix;

$aColumns = array('ID', 'Date', 'Terms', 'AccountID', 'Amount', 'CurrencyID', 'TypeAccount');
//  
//  /* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "ID";
//  
//  /* DB table to use */
 $sTable = $TablePrefix . 'general_ledger';

//  
//  /* Database connection information */
//  $gaSql['user']       = 'root';
//  $gaSql['password']   ='' ;
//  $gaSql['db']         = 'mowena';
//  $gaSql['server']     = "localhost";
//$mySQL = new MySQL(); 
//        $mySQL = new DAL\Database\MySQL();

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP server-side, there is
 * no need to edit below this line
 */

/*
 * Local functions
 */
function fatal_error($sErrorMessage = '') {
    header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
    die($sErrorMessage);
}

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


/*
 * Ordering
 */
//  $sOrder = "ORDER BY CourierID DESC ";
if (isset($_GET['iSortCol_0'])) {
    $sOrder = "ORDER BY  ";
    for ($i = 0; $i < intval($_GET['iSortingCols']); $i++) {
        if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true") {
            $iColumnIndex = array_search($_GET['mDataProp_' . $_GET['iSortCol_' . $i]], $aColumns);
            $sOrder .= $aColumns[$iColumnIndex] . "
                    " . mysql_real_escape_string($_GET['sSortDir_' . $i]) . ", ";
        }
    }

    $sOrder = substr_replace($sOrder, "", -2);
    if ($sOrder == "ORDER BY") {
        $sOrder = "ORDER BY ID DESC";
    }
}
$sOrder = "ORDER BY ID DESC";

/*
 * 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 (";
    for ($i = 0; $i < count($aColumns); $i++) {
        if (isset($_GET['bSearchable_' . $i]) && $_GET['bSearchable_' . $i] == "true") {
            $sWhere .= $aColumns[$i] . " LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%' OR ";
        }
    }
    $sWhere = substr_replace($sWhere, "", -3);
    $sWhere .= ')';
}

/* Individual column filtering */
for ($i = 0; $i < count($aColumns); $i++) {
    if (isset($_GET['bSearchable_' . $i]) && $_GET['bSearchable_' . $i] == "true" && $_GET['sSearch_' . $i] != '') {
        if ($sWhere == "") {
            $sWhere = "WHERE ";
        } else {
            $sWhere .= " AND ";
        }
        $iColumnIndex = array_search($_GET['mDataProp_' . $i], $aColumns);
        $sWhere .= $aColumns[$iColumnIndex] . " LIKE '%" . mysql_real_escape_string($_GET['sSearch_' . $i]) . "%' ";
    }
}
if(isset($_REQUEST['userID']) and $_REQUEST['userID']!=''){
 $sWhere=" WHERE AccountID=".$_REQUEST['userID'];   
}


/*
 * SQL queries
 * Get data to display
 */

 $sQuery1 = "
        SELECT SQL_CALC_FOUND_ROWS " . str_replace(" , ", " ", implode(", ", $aColumns)) . "
        FROM   $sTable
        $sWhere
        $sOrder
        $sLimit
    ";
$rResult = mysql_query($sQuery1) or fatal_error('MySQL Error: ' . mysql_errno());

/* Data set length after filtering */
$sQuery = "
        SELECT FOUND_ROWS()
    ";
$rResultFilterTotal = mysql_query($sQuery) or fatal_error('MySQL Error: ' . mysql_errno());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];

/* Total data set length */
$sQuery = "
        SELECT COUNT(" . $sIndexColumn . ")
        FROM   $sTable
    ";
$rResultTotal = mysql_query($sQuery) or fatal_error('MySQL Error: ' . mysql_errno());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];


/*
 * Output
 */
$output = array(
    "sEcho" => intval($_GET['sEcho']),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iFilteredTotal,
    "aaData" => array()
);


$count = $_GET['iDisplayStart'] + 1;
while ($aRow = mysql_fetch_array($rResult)) {
    $row = array();
    for ($i = 0; $i < count($aColumns); $i++) {
        if ($aColumns[$i] == 'Date') {
            $row[$aColumns[$i]] = Date('Y-m-d', strtotime($aRow['Date']));
        } elseif ($aColumns[$i] == 'Active') {
            if ($aRow[$aColumns[$i]] == 'Y')
                $row[$aColumns[$i]] = 'Yes';
            else
                $row[$aColumns[$i]] = 'NO';
        }elseif ($aColumns[$i] == 'Terms') {
            if(!empty($aRow['Terms'])){
            $row[$aColumns[$i]] = "<textarea style='width:220px;height:50px'>".$aRow['Terms']."</textarea>";}
            else{
                $row[$aColumns[$i]]="---";
            }
        }elseif (!empty($aColumns[$i])) {
            /* General output */
            $row[$aColumns[$i]] = '<font>' . $aRow[$aColumns[$i]] . ' </font>';
        }
    }



    $AccountID = $aRow[AccountID];

    $account = new BLL\Accounts;
    $account->setID($AccountID);
    $account->get($arrayOfacc);
    if (!empty($arrayOfacc)) {
        $arrayOfacc = $arrayOfacc[0];
        $row['AccountID'] = "<a href='user-account.php?accountid=".$aRow[AccountID]."'>".$arrayOfacc->getAccountName()."</a>";
    }
    $Currency = new BLL\Currency();
    $Currency->setCurrencyID($aRow[CurrencyID]);
    $Currency->get($arrayOfCurr);
    if (!empty($arrayOfCurr)) {
        $arrayOfCurr = $arrayOfCurr[0];
        $row['CurrencyID'] = $arrayOfCurr->getCurrencyCode();
    }
    $AccountType = $aRow[TypeAccount];
    switch ($AccountType) {
        case 0:
            $row['Debit'] = $aRow[Amount];
            $row['Credit'] = '-';
            break;
        case 1:
            $row['Credit'] = $aRow[Amount];
            $row['Debit'] = '-';
            break;

        default:
            break;
    }
    $row['Actions'] = '<a id="edittr'.$aRow[ID].'" onclick="edit_ledger(' .$aRow[ID]. ')"> Edit</a>&nbsp;|&nbsp;<a href="user-account.php?accountid='.$aRow[AccountID].'">view all</a>';
    $row['count'] = '<font size=2>' . $count++ . '</font>';

    $output['aaData'][] = $row;
}//deal_paymentstatus($aRow[paymentstatus])

//echo json_encode($output);
//        var_dump($output);
?>

'JSON无法解析'。这是由JSON引起的 enter image description here 你能指导我怎么做才能纠正它吗?

----------------------------------------------错误发生了

我改变了建议的JSON的一部分后

aoColumns = [
{"mData":"count"},{"mData":"AccountID"},
{"mData":"Debit"},{"mData":"Credit"},
{ "mData": "CurrencyID"},
{"mData":"Date"},
{"mData":"Terms"},
{"mData":"Actions"}
]

以下部分代码不起作用

<div class="DTED_Lightbox_Content"  id="DTED_Lightbox_Content1"  tabindex="-1" style="width: 40%;display: none;position:absolute;top:25%;left:25%;z-index: 1000" >
    <h1>amir</h1>
    <div class="DTE DTE_Action_Create">
        <div data-dte-e="processing" class="DTE_Processing_Indicator"  id="DTE_Processing_Indicator1" style="display:none"></div>
        <div data-dte-e="head" class="DTE_Header">
            <div data-dte-e="head_content" class="DTE_Header_Content">Edit Account Group </div>
        </div>
        <div data-dte-e="body" class="DTE_Body">
            <div data-dte-e="body_content" class="DTE_Body_Content" style="height: 400px;">
                <form data-dte-e="form" class="" style="display: block;">
                    <div data-dte-e="form_content" class="DTE_Form_Content">
                        <div id="peymethod1">
                            <table>
                                <tr><td>Account Name:</td><td><select id="account_name1">
                                            <option value="">Select account name </option>
                                            <?php
                                            $Acc = new BLL\Accounts;
                                            $Acc->get($arrayOfObjects);
                                            if (!empty($arrayOfObjects)) {
                                                foreach ($arrayOfObjects as $value) {
                                                    echo "<option value=" . $value->getID() . "> " . $value->getAccountName() . "</option>";
                                                }
                                            }
                                            ?>
                                        </select></td></tr>
                                <tr><td>Date:</td><td><input type="text" id="date3" value="" name="account-date"/></td></tr>
                                <tr><td>Amount:</td><td><input type="text"  value="" name="account-amount" id="amount1"/></td></tr>
                                <tr><td>Account Type:</td><td><select id="account_type1">
                                            <option value="">Select account Type</option>
                                            <option value="0">Debit</option>
                                            <option value="1">Credit</option>
                                        </select></td></tr>
                                <tr><td>Currency:</td><td><select id="currency1">
                                            <option value="">Select Currency</option>
                                            <option value="2">EUR</option>
                                            <option value="1">USD</option>
                                        </select></td></tr> 
                                <tr><td>term:</td><td><textarea name="account_term" id="term1"></textarea></td></tr>
                            </table>
                        </div>

                        <div class="">
                            <div data-dte-e="input" class=""> 
                                <div data-dte-e="msg-message" class="DTE_Field_Message"></div>
                                <div data-dte-e="msg-info" class="DTE_Field_Info"></div></div>
                        </div> 
                        <div data-dte-e="form_error" class="DTE_Form_Error" id="DTE_Form_Error1" style="display: none;">
                        </div>
                        <div data-dte-e="form_clear" class="DTE_Form_Clear" id="DTE_Form_Clear1"></div>
                    </div></form></div>
        </div>
        <div data-dte-e="foot" class="DTE_Footer">
            <div data-dte-e="foot_content" class="DTE_Footer_Content">
                <div data-dte-e="form_buttons" class="DTE_Form_Buttons" id="DTE_Form_Buttons1"><button class="butt">Update</button>
                    <input type="hidden" name="Gid" id="Gid" value=""/>  </div></div></div></div>
    <div class="DTED_Lightbox_Close" id="DTED_Lightbox_Close1" ></div></div>




<div class="ui-widget-overlay" style="display: none;z-index: 1;width: 100%; height: 100%;"></div>


<?php
require_once 'footer.php';
?><br />

任何人都可以指导吗?

3 个答案:

答案 0 :(得分:0)

'sAjaxSource'url必须包含json编码数据

使用http://jsonlint.com/验证json数据

适用于我的版本:

的jquery-1.7.1,    jquery.dataTables 1.9.0

答案 1 :(得分:0)

您的aoColumns json格式错误请删除空格并使用以下格式:

aoColumns = [
{"mData":"count"},{"mData":"AccountID"},
{"mData":"Debit"},{"mData":"Credit"},
{ "mData": "CurrencyID"},
{"mData":"Date"},
{"mData":"Terms"},
{"mData":"Actions"}
]

答案 2 :(得分:0)

问题似乎是这部分:+ $('#users')。val()

当我把它留下来时,json格式似乎有效,如果我用在线json阅读器检查它(我使用http://jsonviewer.stack.hu/但有很多)。

可能是的原因是你不能在json字符串中添加javascript / jquery代码。