Invalid Json response

Invalid Json response

CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0

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

  • kthorngrenkthorngren Posts: 15,787Questions: 25Answers: 3,739
    Answer ✓

    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

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0

    Yes i did follow but i´m not getting this to work

  • kthorngrenkthorngren Posts: 15,787Questions: 25Answers: 3,739

    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

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0
    edited May 12

    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
    }]
    }

  • kthorngrenkthorngren Posts: 15,787Questions: 25Answers: 3,739

    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

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0

    I get the same response if i try to load from a file.txt

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0

    let me check why i'm getting html response on ajax response.

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0
    edited May 12

    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.

  • kthorngrenkthorngren Posts: 15,787Questions: 25Answers: 3,739
    edited May 12

    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 the draw parameter sent in the request. In your screenshots draw is 1. 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

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0

    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

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0

    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.

  • allanallan Posts: 55,865Questions: 1Answers: 8,830 Site admin

    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

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0

    Hi Allan,

  • allanallan Posts: 55,865Questions: 1Answers: 8,830 Site admin

    Thank you! That shows that the response coming from your server (the index.php/totale/list?limit=20 url) is an HTML document:

    < !DOCTYPE html > < html lang = "en" > < head > < meta charset = "utf-8" > < meta name = "viewport"
    content = "width=device-width, initial-scale=1, shrink-to-fit=no" > < title > Bootstrap Navbar Dropdown Login and Signup Form with Social Buttons < /title>
    <link rel="stylesheet" href="https:/ / fonts.googleapis.com / css ? family = Varela + Round ">
    <link rel="
    

    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

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0

    Thanks Allan,

    Let me try to make this in other environment, i'll let you know.

    Nuno

  • CodeDeviserCodeDeviser Posts: 11Questions: 1Answers: 0
    edited May 12

    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

Sign In or Register to comment.