0

I try using different instructions to use a JSON API from a Wordpress-System in a HTML-Teamplate. Unfortunately I do not succeed. Does anyone have any idea how I can read the section "Content" of http://www.earnyour21.de/api/get_page/?id=1588?

blog: function () {
    $.ajax({
        url: 'http://www.earnyour21.de/api/get_page/?id=1588',
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data);
        }
    });
}
2
  • Do you just need to know how to access the content object within the data via JavaScript? If so will the structure of the json always be the same? Commented Oct 10, 2014 at 19:59
  • Yes, I think the structure of JSON always be the same. Thanks Commented Oct 10, 2014 at 20:04

1 Answer 1

1

If the data structure of the JSON will always be the same, you can simply access the object directly using the objects name in JS.

blog: function(){
 $.ajax({
url: 'http://www.earnyour21.de/api/get_page/?id=1588',
type: 'GET',
dataType: 'json',
success: function(data){
  $('#content_test').append(data['page']['content']);
 },
 error: function(data){
  $('#content_test').append(data['page']['content']);
 }
});
}

Basically you need to use jquery to grab the div with an id of content_test and then append your data from the json. http://api.jquery.com/append/ and http://www.json.com/ for further reference.

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

11 Comments

Thank you very much. But how can I evaluate this in html? For example: <div id="content_test"></div>
So you would like to add the text within the JSON content object to a div so it would look like this: <div id="content_test"><p>start<\/p></div>?
Yes, exactly this is what I need to do
you are the best. Thank you very very much! :-) Still one question: Why gets JSON instead of </p> this: <\/p>
I did not add the <p> tags because they were already included in the JSON content object. If content only had the text you could add it like: $('#content_test').append('<p>'+data['page']['content']+'</p>');` however there are many different ways to do that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.