-2

I'm working on server api and scripts send/receive data in JSON format, but when I try to parse received data, I got "undefined", here sample data:

[{"id":"1","name":"Item 1","description":"","like_btn":"Burn in the hell, bitch!","dislike_btn":"Forgive"},{"id":"2","name":"Item 2","description":"","like_btn":"Like it","dislike_btn":"Happens worse"},{"id":"3","name":"Item 3","description":"","like_btn":"Explosed.","dislike_btn":"You're really stupid!"},{"id":"4","name":"Item 4","description":"","like_btn":"You're in deep shit!","dislike_btn":"Sometimes it happens."}]

Here is JS code:

function DataTransmitter()
{
    this.backendUrl = window.location.origin + '/backend/';

    /**
     * Send GET request to the selected URI
     * @param {string} uri
     * @returns {json}
     */
    this.get = function(uri)
    {
        $.ajax({
            url: this.backendUrl + uri,
            dataType: 'json',
            success: function(data)
            {
                return JSON.parse(JSON.stringify(data));
            }
        });
    }

    /**
     * Return object with all post types
     *
     * @returns {json}
     */
    this.getPostTypes = function()
    {
        return this.get('feed');
    }

    /**
     * Get posts from feed with selected type (default: best)
     *
     * @param {string} type
     * @returns {json}
     */
    this.getFeed = function(type)
    {
        var data;

        if(!type)
            type = 'best';

        if(type == 'best' || type == 'fresh')
        {
            data = this.get('feed/' + type);
        }else{
            data = this.get('feed/type/' + type)
        }

        return data;
    }
}

function AppApi()
{
    this.transmitter = new DataTransmitter();
    this.postTypes = null;

    this.getFeed = function(type)
    {
        var data = this.transmitter.getFeed(type);
        return data;
    }

    this.getPostTypes = function()
    {
        if(this.postTypes === null)
        {
            this.postTypes = this.transmitter.getPostTypes();
        }

        return this.postTypes;
    }
}

api = new AppApi();
console.info(api.getPostTypes()); //<------ Here is problem now

Please, help me! How can I fix this error?

Rakshazi
  • 75
  • 1
  • 10
  • Search for the error message (tldr; the problem is it *already* is a *JavaScript object*, not JSON which is text - don't parse it) – user2864740 Sep 09 '14 at 05:45
  • Also http://stackoverflow.com/questions/14432165/uncaught-syntaxerror-unexpected-token-with-json-parse anyway .. *search* for error messages. – user2864740 Sep 09 '14 at 05:47
  • now script shows only "undefined", recheck the code please – Rakshazi Sep 09 '14 at 06:26
  • Read the duplicates; the reason and solution was "missed". The code **has** the JavaScript object in the success callback, don't turn it in to JSON to turn it back to a JavaScript object.. – user2864740 Sep 09 '14 at 06:37
  • The problem that is currently being faced is discussed in http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call - a value can't be "returned" from the success function (furthermore the outside `get` function itself *has no return value*), so any usage of `get(x).foo` is nonsense. – user2864740 Sep 09 '14 at 06:39

1 Answers1

2

dataType: 'json' yet do JSON.parse() Error throwing because JSON try to parse this string [object Object]

cevek
  • 862
  • 1
  • 7
  • 16