JSON Data from server could not be parsed this is caused by JSON formatting error

JSON Data from server could not be parsed this is caused by JSON formatting error

rushiwebrushiweb Posts: 10Questions: 3Answers: 0

I am getting JSON formatting error but i am sure that data i am sending from server is valid because i have validated it on jsonlint. what other reason could cause this error?

Answers

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

    Please either use the debugger or provide a link to a page showing the issue.

    http://datatables.net/forums/discussion/12899/post-test-cases-when-asking-for-help-please-read

  • rushiwebrushiweb Posts: 10Questions: 3Answers: 0

    ok for more details i will provide some code:
    data from server is exaclty:
    [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]

    and at the client end:

    $('#report-table').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "/omnisign/reports/network/get_report/"
    });

    i am pretty much sure that data i am sending from server is a valid JSON. please help resolve this issue

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

    That doesn't help. Please refer to my previous post.

  • rushiwebrushiweb Posts: 10Questions: 3Answers: 0

    i am unable to host any example on jsfiddle because data is coming from my localhost.
    i just want to ask that aaData i am sending from server is valid JSON data even at the client end i have verified that aaData is valid JSON but still datatables gives me warning that JSON data is invalid. sorry for short information

  • allanallan Posts: 63,747Questions: 1Answers: 10,509 Site admin

    If you are getting that error then the simple fact is that the data being received is not valid JSON. Although it is DataTables that is emitting the warning it is because jQuery threw an error while trying to parse the JSON - and jQuery's JSON parser is quite reliable!

    It is possibly you might have a hidden BOM in your returned data, or there might be something else happening, but without a test case or at least a debugger trace there is very little help we can offer.

    Allan

  • rushiwebrushiweb Posts: 10Questions: 3Answers: 0
    edited September 2015

    i can send debug data to you. is it feasible to trace problem from that?

  • allanallan Posts: 63,747Questions: 1Answers: 10,509 Site admin

    I can certainly see what is being returned from the server. Normally it will contain an error message or something else that is making it invalid.

    Allan

  • rushiwebrushiweb Posts: 10Questions: 3Answers: 0

    ok thanks a lot for quicker help. right now i can provide u debug data link:
    http://debug.datatables.net/atanar
    if possible please let me knw wats going wrong. thanks again

  • allanallan Posts: 63,747Questions: 1Answers: 10,509 Site admin

    The Ajax request to "/omnisign/reports/network/get_report/" is returning:

    aaData
    

    Just that. No quote marks, no other formatting of any other kind. That is most certainly not JSON :-).

    You must have seen JSON somewhere I guess since you said it was coming back as valid JSON, so I guess the question is, where is it? Also, why is the get_report script not returning valid JSON. That is the issue - the error message is exactly correct.

    Allan

  • rushiwebrushiweb Posts: 10Questions: 3Answers: 0

    my aaData dictionary contains the JSON which i am sending from server but i am not able to find why does datatables won't find same. thanks for quicker help again

  • allanallan Posts: 63,747Questions: 1Answers: 10,509 Site admin

    The server is simply sending back the string aaData. It is not sending back JSON. If aaData is a variable you need to output it as JSON rather than the variable's name :-)

    Allan

  • rushiwebrushiweb Posts: 10Questions: 3Answers: 0
    edited October 2015

    Ok thanks again, now i would like to show some more piece of code here:
    my server side is python-django and my view contains -

    tmp_list = [[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]
    jsonlist = json.dumps(tmp_list)
    return HttpResponse({'aaData':jsonlist })

    and on HTML -

    $(document).ready(function() {
    $('#report-table').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "/omnisign/reports/network/get_report/"
    });
    })

    at server end i am simply returning a json data.
    i belive that i have followed datatables basic configuration and initialization. please

  • allanallan Posts: 63,747Questions: 1Answers: 10,509 Site admin

    I'm afraid I don't know Python at all so I can't offer any help there. All I can say is that the debug trace you gave above shows that the string aaData is all that is being returned. To check that, simply load up your debug trace, click the "Tables" tab and then the "Server interaction" section.

    If you need help returning JSON from a Python script I would suggest asking in a Python forum as that is not something I can offer any help with.

    Allan

  • rushiwebrushiweb Posts: 10Questions: 3Answers: 0
    edited October 2015

    thanks a lot allan for ur help. I got my mistake. i should had done:
    resp = {'aaData':jsonlist}
    return HttpResponse(json.dumps(resp, cls = DjangoJSONEncoder))

    This resolved my error. thanks again

  • allanallan Posts: 63,747Questions: 1Answers: 10,509 Site admin

    Excellent - thanks for posting back. Good to hear you've got it working now.

    Allan

This discussion has been closed.