2

When I call an api that returns a random quote in json format (a title and content), I receive the json just fine :

ajax({ url: 'quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1', type: 'json' }, 
function(data) {
console.log(( JSON.stringify( data ) ));
console.log(data.content);

console.log(( JSON.stringify( data ) )); 

outputs :

[{
   "ID":1562,
   "title":"Michael Bierut",
   "content":"Most of the mediocre design today comes from designers who are faithfully doing as they were taught in school: they worship at the altar of the visual.\n",
   "link":"http:\/\/quotesondesign.com\/michael-bierut-3\/",
   "custom_meta":{"Source":"article"}
}]

But console.log(data.content); outputs : none.

3
  • 1
    The root JSON value doesn't have a "content" property to match data.content. It appears data is an Array – console.log(data[0].content), etc. (Access / process (nested) objects, arrays or JSON) Commented Apr 21, 2016 at 4:46
  • @RayonDabre It isn't clear that this is jQuery. Despite some similarities, the arguments don't appear to be compatible with jQuery.ajax(). Commented Apr 21, 2016 at 4:55
  • @JonathanLonowski, True! Commented Apr 21, 2016 at 4:56

3 Answers 3

2

try console.log(data[0].content);

Sign up to request clarification or add additional context in comments.

Comments

1

I believe the issue is that you are stringifying instead of parsing

console.log(JSON.parse(data));

Edit: The example on the question has some other problems as well. Here is a correctly formatted ajax request:

$.ajax({
  type: 'GET',
  dataType: 'json',
  url: 'URL_HERE',
  success: function(data) {
    console.log(data.content);
  }  
});

and a working example https://jsfiddle.net/1fjqgajk/8/

1 Comment

The ajax request that i did is based on that of the pebble, the way i did is it the way to suggest to do it.
0
$.ajax({url:"quotesondesign.com/wp-json/posts?  filter[orderby]=rand&filter[posts_per_page]=1",type:'POST',dataType="json",success: function(data){
// if you dont wanto to use dataType use eval;
 //var jsonObj=eval(data)

}});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.