Invalid json response from Server for the following json data

Invalid json response from Server for the following json data

SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

Hello Every,

I am Satya trying to get data from mysql, the values are dynamically sent to mysql server by encoding them in where clause, now I got the following k ind of data that is being reported as invalid json format, but I have searched mny foruma where they have same data working for datasets, is there anyone to suggest me any solution/idea?

{"sEcho":0,"iTotalRecords":0,"iTotalDisplayRecords":0,"aaData":[["Bank Deposits","-90853.0000","98.0000","0.00000000","-90853.0000"],["Classes","238458.6400","1398.0000","792.28000000","282140.4590"],["Diving Equipment","928994.2600","12495.0000","534050.05400000","541323.8660"],["Gift Cards","45966.6200","268.0000","0.00000000","48727.5000"],["Miscellaneous","171459.4900","13867.0000","58392.81160000","539491.8784"],["Pay Outs","-19631.0600","196.0000","0.13000000","-17849.6564"],["Rentals","39554.8300","852.0000","60.11000000","39180.2530"],["Servicing","131207.8100","11559.0000","43991.04760000","132605.8424"],["Student Materials","403.0900","417.0000","0.00000000","403.0900"],["Travel","464629.3700","816.0000","260.00000000","819789.4900"]]}

Thanking you

Satya Achanta

Answers

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited October 2014

    Your json reply data looks like this: "aaData":[ [ ] ] and I believe it should look like this: "aaData":[ { } ]

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0
    edited October 2014

    I tried a lot to get in that format, but I am unable to do, do you have any suggestion about getting aaData in that format. @ignignokt

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited October 2014

    It would help if you showed your PHP code for building the reply. Here is how you create the two different types though.

    What you have:

    echo json_encode(array(array('column1value','column2value')));
    

    Results in: [["column1value","column2value"]]

    What you need to have:

    echo json_encode(array(array('column1key'=>'column1value','column2key'=>'column2value')));
    

    Results in: [{"column1key":"column1value","column2key":"column2value"}]

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0
    edited October 2014

    Here is my php code

    <?php
    include 'dataLogin.php';
    include 'dbcon.php';
    error_reporting ( E_ALL ^ E_NOTICE );
    if (isset ( $_POST ['companyid'] ))
    {

    $company = $_POST ['companyid'];
    $start = $_POST ['startdate'];
    $to = $_POST ['enddate'];
    $aColumns = array (
            'profitcenter',
            'grosssales',
            'qty',
            'COGS',
            'grossmargin' 
    );
    
    $data_sql = mysqli_query (--------- );
    
    $output = array (
            "sEcho" => intval ( $_GET ['sEcho'] ),
            "iTotalRecords" => 0,
            "iTotalDisplayRecords" => 0,
                        "aaData" => array ()
    );
    
    if ($data_sql) {
        $row = array ();
        while ( $aRow = mysqli_fetch_array ( $data_sql ) ) {
            for($i = 0; $i < count ( $aColumns ); $i ++) {
                if ($aColumns [$i] != ' ') {
                    // Charset for accentuation.
                    $row [$i] = $aRow [$aColumns [$i]];
                }
            }
            $output ['aaData'] [] = $row;
        }
        //echo json_encode($output) ;
                echo json_encode($output);
    } else {
    
        echo die ( "The error is:" . mysqli_error ( $LyticsDB ) );
    }
    

    }

    <?php > ?>
  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    I am sorry I am just wondering how to format that code .

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited October 2014

    I'm not entirely sure what you are trying to do, and it is hard to read your code in a big paragraph block. But your mysqli_fetch_array should probably just look like this instead of your for($i = 0 loop and stuff.

    while($aRow = mysqli_fetch_array($data_sql)){
        $output['aaData'][] = $aRow
    }
    
  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    I can send you the email if it is okay for you, I am new to this website so I am feeling consufed about formatting....

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    I have created fiddle without any functionality for that code here, can you please take a look at this http://jsfiddle.net/satya_achanta/acqmvw06/

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited October 2014

    I don't see the purpose of the for loop in the while loop. Try replacing line 37-46 in your fiddle with the while loop I put in my previous comment.

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    yes I did, when I removed the for loop then it is like below
    if ($data_sql) {
    $row = array ();
    while ( $aRow = mysqli_fetch_array ( $data_sql ) ) {
    $output ['aaData'] [] = $aRow;
    }
    echo json_encode($output);
    }
    the data is of form

    {"sEcho":0,"iTotalRecords":0,"iTotalDisplayRecords":0,"aaData":[{"0":"Bank Deposits","profitcenters":"bankdeposits","1":"-90853.0000","grossmargin:"-90853.0000"........}.here what is happening is the data is getting repeated either I have to get "0":"bankdeposits" or "profitcenters":"bankdeposits" but I am getting both in the same array

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited October 2014

    Also change line 32 in the fiddle to "aaData" => array()

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0
    edited October 2014

    The same thing is happening, getting the data repeated......we need for loop inside it otherwise on what basis I can search the elements, I store the elements on the names of arrays that are equal to column names in the database, if we dont have for loop it is mistake I believe. If I am wrong correct me.

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39

    Change mysqli_fetch_array to mysqli_fetch_assoc

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    not working man......I don't know where the error occurs, it seems everywhere it is good but not working....

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39

    The issue of duplicate data is because you are using fetch_array instead of fetch_assoc. If you are still getting duplicate data after changing that I don't know what to tell you.

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    I did that..okay I will figure it out and once I got that I will post the solution.

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    Okay I got in the format you suggested me , now data without repetitions and it is in the format of {"aaData":[{"profitcenters":"bankdeposits","Grosssales":"12345.67"........}]}, but still getting invalid json error...

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    If you are getting the invalid JSON error, there is something being returned that is making the data invalid JSON. That might be an error message or something else. This tech note might be of use to determine what it is. Otherwise we would need either a debugger trace or a link to the page.

    Allan

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0
    edited October 2014

    Hello Allan, here is my debugger code "ewayed" (without double quotes), I am trying to get the values from the database and displaying in the tables on the button click, I tried using Ajax alone but it doesn't seems like good way for me, so I started using server side scripting. If you have any suggestion in your mind please let me know.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    The server shows that no data was returned by the server:

    "responseText": "
    ";

    That is not valid JSON, hence the error.

    Allan

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0
    edited October 2014

    Hello Allan, but when I alert the data that came from another page, there is some data from the other page, but I don't know why it is not getting displayed in the datatable, where did you find the resposne text is empty, please let me know, by the way this is the response text what I got from the previous page, I have found this in developer tools of google chrome
    {"aaData":[{"profitcenter":"Bank Deposits","grosssales":"-90853.0000","qty":"98.0000","COGS":"0.00000000","grossmargin":"-90853.0000"},{"profitcenter":"Classes","grosssales":"238458.6400","qty":"1398.0000","COGS":"792.28000000","grossmargin":"282140.4590"},{"profitcenter":"Diving Equipment","grosssales":"928994.2600","qty":"12495.0000","COGS":"534050.05400000","grossmargin":"541323.8660"},{"profitcenter":"Gift Cards","grosssales":"45966.6200","qty":"268.0000","COGS":"0.00000000","grossmargin":"48727.5000"},{"profitcenter":"Miscellaneous","grosssales":"171459.4900","qty":"13867.0000","COGS":"58392.81160000","grossmargin":"539491.8784"},{"profitcenter":"Pay Outs","grosssales":"-19631.0600","qty":"196.0000","COGS":"0.13000000","grossmargin":"-17849.6564"},{"profitcenter":"Rentals","grosssales":"39554.8300","qty":"852.0000","COGS":"60.11000000","grossmargin":"39180.2530"},{"profitcenter":"Servicing","grosssales":"131207.8100","qty":"11559.0000","COGS":"43991.04760000","grossmargin":"132605.8424"},{"profitcenter":"Student Materials","grosssales":"403.0900","qty":"417.0000","COGS":"0.00000000","grossmargin":"403.0900"},{"profitcenter":"Travel","grosssales":"464629.3700","qty":"816.0000","COGS":"260.00000000","grossmargin":"819789.4900"}]}

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    where did you find the resposne text is empty, please let me know

    http://debug.datatables.net/ewayed

    Click the 'Tables' tab and then 'Server interaction'.

    You can also use 'Full table state' and search for the responseText which is a directly copy of the XHR object from jQuery. It contains an empty string from a 200 OK response.

    Allan

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    Hello Allan, I tried to fix that, but it seems everything is good for me, I am sending output data in php using json_encode but data gets into the page but not into the datatable, as I am naive to this functionality I am unable to find where I went wrong, can you suggest me where I went wrong by looking at this fiddle, this is just code without any functionality, I am unable to format the code in this comment box so I created static fiddle http://jsfiddle.net/satya_achanta/acqmvw06/, this is the fiddle for the php page where I send the output.

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0
    edited October 2014

    Hello @Allan, I have some weird problem, I tired in the way you suggested me in the morning, to know how it works, I used static values in the where clause of sql query, the debugger code for that is "examag" (without quotes) and next I have sent few values to mysql query using ajax from another page, now it doesn't work the debugger code for that is "
    ayirok", but for me the data returned is in both format in both the cases, I am not sure where the problem occurs, if you have any idea in your mind please suggest me.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Look at "Server interaction" from your debugger ref. ayirok.

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    Hello Tangerine, I looked at that, I am unable to read that and inturn I am getting data to the page but why it doesn't comes to datatables, if I use static values it works,

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Why can't you read it?

    It says you have a bunch of undefined stuff in datasample.php.

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0

    I mean I am unable to know why it is, in fact those are the variables that are coming using Ajax request from the previous page, even the required page receives their values uing Ajax but not reading them, if I again check them by sending back the received values through json_enocde they are getting displayed.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    edited October 2014

    We don't know what is in your script so can't say what is wrong with it. Perhaps you are trying to read from _POST but the request being sent is _GET, but really that is just a guess without having all the information needed to be able to debug it.

    Allan

  • SatyaAchantaSatyaAchanta Posts: 22Questions: 3Answers: 0
    edited October 2014

    Hello Allan, You are correct, I have sent request through post and I am receiving through get, I have observed my console, when I sent ajax request it is post, but when I get data it is get, here is the debugger code for that updated script "evadah"

This discussion has been closed.