How to debug invalid JSON when no response (at all, no error, no nothing) is returned?

How to debug invalid JSON when no response (at all, no error, no nothing) is returned?

EcoDevEcoDev Posts: 2Questions: 1Answers: 0

Hi there,

I've been having great success with a very large dataset, using server side processing and ajax. I'd gotten quite deep into my application when I started encountering a problem. In some cases, my datatables won't load. I get the usual "1. Warning: Invalid JSON response" error, BUT...when I go to debug it by looking at the response...there is no response. Nothing, no error, no invalid json. Just "This request has no response data available". I can make it return something (like echo("something");), but it doesn't return any errors. And if I make it return the select statement for the records, it is a valid select statement.

I will try to get a test case or a link up and running, but both would be really difficult. My site is password protected and I have no idea how to disentangle the pages/classes/web-service interactions into a test case. The biggest problem is that the bug is inconsistent. Most of the time, it works. But for some datasets, it doesn't. And I can't SEE a difference between the datasets, but then, I can't see the JSON response, so there could be something about some particular datasets that just isn't right.

Anyway! The questions are: any advice on how to debug it now? What kinds of bugs make a server return no response? I'd take advice on short cuts to making test cases too.

I do have these from the debugger:
Here is a page with a dataset that loads properly: http://debug.datatables.net/acoqax
This is the same page, but the table won't load this different dataset, no error: http://debug.datatables.net/omugoh

Thanks for any advice you might have, and I'm sorry I don't know how to actually show you the problem.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,934Questions: 1Answers: 10,155 Site admin

    there is no response. Nothing, no error, no invalid json

    Actually, no response is not valid JSON. So the issue is that nothing is being returned. As to why that is, you'd need to debug the server-side script, perhaps take a look at the server's error log.

    Allan

  • glendersonglenderson Posts: 231Questions: 11Answers: 29
    Answer ✓

    Are you examining the actual response body from the ajax request? Are you including the condition for handling an empty recordset in your code? "data: []". I've gotten to the point were I include both the SQL statement and the records in the ajax response.

    For a full table my JSON response would be something like this if the recordset is empty.

    {
    "sql":"... the SQL string ..."
    , data: []
    }

    A recordset collision could make the SQL work "sometimes" and the results is hidden because the response is an error message instead of valid JSON data. But, the response body should show that.

    If you are seeing nothing in your response body, then chances are you are not handling no records being returned condition. To view your response body, I would suggest (if you are not already using it), the browsers developer features. F12 in IE and Chrome, FireBug in FF.

  • EcoDevEcoDev Posts: 2Questions: 1Answers: 0

    Thank you both for your responses, I think they got me started down the right path. I had been debugging everything I could think of in the server side script, but putting the SQL into the JSON response is brilliant. In the process of doing that, I realized an obvious fail point I hadn't even considered is PHP json_encode function I was using. It only works when strings are UTF-8 encoded, and I probably just haven't properly encoded one of my string values. Headdesk. Thanks again for your help. The net lesson is that if no JSON is being returned, your json_encode function is probably not working.

This discussion has been closed.