Invalid Json response
Invalid Json response
![CodeDeviser](https://secure.gravatar.com/avatar/a756d76504f8647ef55536f3c5d8bca9/?default=https%3A%2F%2Fvanillicon.com%2Fa756d76504f8647ef55536f3c5d8bca9_200.png&rating=g&size=120)
Link to test case:
Debugger code (debug.datatables.net):
Error messages shown: No errors, i'm getting 200 ok response
Description of problem: Hi there, i have tried datatables examples serverside and without serverside, i'have search a few examples of datatables working here and outside this forum, and none of them work, i allways get invalid json response.
Let me share my code:
$(document).ready(function() {
$('#t').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url":"http://localhost:8888/api-ls/index.php/totale/list?limit=20",
"type": "GET",
"dataType": "json",
},
"columns": [
{"data": "id"},
{"data": "plataforma"},
{"data": "cliente"},
{"data": "articulo"},
{"data": "tot_stock"},
{"data": "total_lineas"}
],
} );
} );
Name | Position | Office | Extn. | Start date | Salary |
---|
{
"draw": 1,
"recordsTotal": 57,
"recordsFiltered": 57,
"data": [
[
"Airi",
"Satou",
"Accountant",
"Tokyo",
"28th Nov 08",
"$162,700"
],
[
"Angelica",
"Ramos",
"Chief Executive Officer (CEO)",
"London",
"9th Oct 09",
"$1,200,000"
],
[
"Ashton",
"Cox",
"Junior Technical Author",
"San Francisco",
"12th Jan 09",
"$86,000"
],
[
"Bradley",
"Greer",
"Software Engineer",
"London",
"13th Oct 12",
"$132,000"
],
[
"Brenden",
"Wagner",
"Software Engineer",
"San Francisco",
"7th Jun 11",
"$206,850"
],
[
"Brielle",
"Williamson",
"Integration Specialist",
"New York",
"2nd Dec 12",
"$372,000"
],
[
"Bruno",
"Nash",
"Software Engineer",
"London",
"3rd May 11",
"$163,500"
],
[
"Caesar",
"Vance",
"Pre-Sales Support",
"New York",
"12th Dec 11",
"$106,450"
],
[
"Cara",
"Stevens",
"Sales Assistant",
"New York",
"6th Dec 11",
"$145,600"
],
[
"Cedric",
"Kelly",
"Senior Javascript Developer",
"Edinburgh",
"29th Mar 12",
"$433,060"
]
]
}
This question has an accepted answers - jump to answer
Answers
Did you follow the troubleshooting steps at the link in the error?
https://datatables.net/manual/tech-notes/1
What did you find in the XHR response?
Kevin
Yes i did follow but i´m not getting this to work
Please provide more details of what you aren't able to make work. Does that mean you aren't seeing a response in the XHR Response tab or something else?
Kevin
I'm getting this:
My Json response:
{
"draw": 0,
"recordsTotal": 4,
"recordsFiltered": 4,
"data": [{
"id": 1,
"plataforma": "CG",
"cliente": "1160",
"articulo": "12293457",
"tot_stock": 347403,
"total_lineas": 725
}, {
"id": 2,
"plataforma": "CG",
"cliente": "1160",
"articulo": "12168420",
"tot_stock": 314091,
"total_lineas": 657
}, {
"id": 3,
"plataforma": "CG",
"cliente": "1160",
"articulo": "12487249",
"tot_stock": 891651,
"total_lineas": 608
}, {
"id": 4,
"plataforma": "CG",
"cliente": "1160",
"articulo": "12329675",
"tot_stock": 98011,
"total_lineas": 606
}]
}
Looks like your server script is returning the HTML web page in the response - not the JSON data you are expecting. Sounds like something is not setup correctly in your environment or you are using the wrong url in the
ajax
option.Kevin
I get the same response if i try to load from a file.txt
let me check why i'm getting html response on ajax response.
I'm still getting html on response, don't know why, but now i don't have the invalid json error anymore, instead i'm getting "no matching records found"
What i'have changed was "dataType" to "text", if i leave "dataType" to "json" i get the error again.
When i var_dump my response, i'm getting my json response on serverside, don't know how Chrome tells that i'm returning html as response.
What happens if you open the
ajax
url in a web browser?http://localhost:8888/api-ls/index.php/totale/list?limit=20
Additionally I noticed your JSON has
"draw": 0,
which is not valid. This is expected to match thedraw
parameter sent in the request. In your screenshotsdraw
is1
. See the Server Side Processing Protocol docs for more details.Do you need server side processing? See the Processing Modes docs for information about choosing to use server side processing.
Kevin
If i open in a browser, i get invalid json response.
i have tried to change "draw" to 1 aswell but no luck. In my ajax call i already put processing and serverside true, and tried to use ssp class aswell but i still get invalid json response.
Thanks for your time, i'm still looking for a solution and debugging.
Nuno
Maybe the problem is somewhere in the columns, because i'have double checked everything in my server side, my json is valid everything seems well configured.
Can you use the debugger to give me a trace please - click the Upload button and then let me know what the debug code is.
Thanks,
Allan
Hi Allan,
![](https://datatables.net/forums/uploads/editor/ly/l0agd6xf7hyw.png)
Thank you! That shows that the response coming from your server (the
index.php/totale/list?limit=20
url) is an HTML document:That matches what your first image above shows - the response is not valid JSON - it is HTML.
I'm not sure where you are seeing the JSON part that you pasted in?
Allan
Thanks Allan,
Let me try to make this in other environment, i'll let you know.
Nuno
Allan,
I get it to work, instead of calling "index.php/totale/list?limit=20" that was returning an html response somehow, i changed to a file "getData.php" and handled there the response, and another thing, i had to change my data structur from:
{
"id": 2,
"plataforma": "CG",
"cliente": "1160",
"articulo": "12168420",
"tot_stock": 314091,
"total_lineas": 657
},
...
To:
[
[
2,
"CG",
"1160",
"12168420",
314091,
657
],
...
]
So nothing's wrong with datatables, it was on me.
Now it's working, thank you for your time Allan and kthorngren.
Best,
Nuno