JSON format?

JSON format?

handbuchhandbuch Posts: 55Questions: 0Answers: 0
edited September 2010 in General
Hi, i got a php script which gives me a json string. if i echo that string and save it to a file and do

$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": 'file'
} );

it works. but if i return that string and do

$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": 'backend.php'
} );

it doesnt work, why? i get an error DataTables warning: JSON data from server failed to load or be parsed. This is most likely to be caused by a JSON formatting error. but tahts not possible since it works manually.

Replies

  • handbuchhandbuch Posts: 55Questions: 0Answers: 0
    Got it, it must be a numeric array when you make an string with php encode...^^
  • fides5566fides5566 Posts: 3Questions: 0Answers: 0
    Hello, I current use MVC and got a same problem.
    Below is my JSON result.
    [code]
    {"sEcho":1
    ,"iTotalRecords":11
    ,"iTotalDisplayRecords":10
    ,"aaData":[
    ["1","MDH","MDS - API"],["2","P2PS","P2PSClient"]
    ,["3","RMDS","teswt"],["4","RWDS","test"]
    ,["4","RWDS","test"],["4","RWDS","test"]
    ,["4","RWDS","test"],["4","RWDS","test"]
    ,["4","RWDS","test"],["4","RWDS","test"]
    ,["4","RWDS","test"]
    ]}
    [/code]

    I use JSONResult to return data back to the page
    [code]
    return Json(result, "application/json");
    [/code]

    The "result" is an object of below class.
    [code]
    public class DataTableResult
    {
    public int sEcho { get; set; }
    public int iTotalRecords { get; set; }
    public int iTotalDisplayRecords { get; set; }
    public List aaData { get; set; }
    }
    [/code]
    I don't have a clue of what I done worng. Please help!!

    P.S. Sorry for my bad english
  • handbuchhandbuch Posts: 55Questions: 0Answers: 0
    how do you use the json data?
    that my json:

    {"aaData":[["1","qwe","123"],["2","qwe","123"],["3","qwe","123"]]}

    and i use it in jquery with "sAjaxSource": "files/php/getJSON.php"

    i would guess your issue is at

    "sEcho":1
    02.,"iTotalRecords":11
    03.,"iTotalDisplayRecords":10
    04.,"aaData"

    im a totally newbie but i think it is sEcho OR aaData. delete the aaData. But im rly a newbie :p but maybe it helps. post the code where you use the json data.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    The JSON itself looks okay as far as I can see - but open up Firebug and runt he JSOn that you get back through http://jsonlint.com - that will say where the problem is (assuming there is one - which there should be :-) ).

    Allan
  • fides5566fides5566 Posts: 3Questions: 0Answers: 0
    I found a solution anyway XD.
    Just change return statement into this
    [code]
    return Json(result, JsonRequestBehavior.AllowGet);
    [/code]

    I don't really know why, maybe because DataTables use request method as GET?
    Anyway, thank you for replies :D
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yes, DataTables uses GET by default. It is possible to have it switch to POST if you wish ( http://datatables.net/examples/server_side/post.html ).

    Allan
This discussion has been closed.