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?