"dataSrc" returns unaltered JSON result, but it doesn't work.

"dataSrc" returns unaltered JSON result, but it doesn't work.

kgonenowkgonenow Posts: 7Questions: 3Answers: 1
edited March 2016 in Free community support

Hello,

I have a web-application, it uses the ajax parameter for requesting and inserting JSON data into the table.

However at the top of the requested .php file it is checked whether the user is logged in, if not the script encodes a different array: { logged: "false" } .

Normally there isn't an issue however when the user has multiple tabs open and the session is destroyed on just one of them the other tabs break (because of logged in check).

I wanted to catch the result { logged: "false" } and handle it accordingly. For this I want to use the dataSrc property.

I succeeded in this endeavor, however the function doen't return the result properly anymore.

The important piece of code:

"ajax": {
    "url": "php/get_issues.php" + "?id=" + id + "&customer_id=" + customerid,
    "dataSrc": function ( json ) {
        if(typeof json['logged'] != "undefined") {
            if (json['logged'] == 'false') {
                location.replace('php/logout.php');
            }
        }
        return json;
    }
},

The .php script returns data like this: http://i66.tinypic.com/a3zi1x.png

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,917Questions: 1Answers: 10,151 Site admin

    You probably need to use return json.data;. If you use ajax.dataSrc as a function it expects you to return the data array, not the original object.

    Allan

  • kgonenowkgonenow Posts: 7Questions: 3Answers: 1

    json.data is undefined. JSON.stringify(json) returns the json string.

  • allanallan Posts: 61,917Questions: 1Answers: 10,151 Site admin

    Perhaps you could let me know what the structure of json is when valid data is returned then please? Also a link to the page, as requested in the forum rules, would be useful to let me debug and address the issue.

    Allan

  • kgonenowkgonenow Posts: 7Questions: 3Answers: 1
    edited March 2016

    The .php script returns data like this: http://i66.tinypic.com/a3zi1x.png The script contains the proper application/json header.

    I am unable to post an proper working link because the web page isn't online and because it contains corporate data. I will try and get some more info that might be relevant.

    Thanks for supporting me.

    Edit:

    If i completely comment out the dataSrc property, it works like a charm.

  • allanallan Posts: 61,917Questions: 1Answers: 10,151 Site admin
    Answer ✓

    So just use return json.aaData in that case :-). As I said, return the data array.

    Allan

  • kgonenowkgonenow Posts: 7Questions: 3Answers: 1

    Thanks alot! I never though .data was referencing to the first array item (which in this case is also an array).

    Again, thanks a lot!

This discussion has been closed.