0

I am working on implementing a "load more articles" functionality using Python, Flask and Ajax. Everything is working from the server-side but I don't know how to append the new data to the HTML by using jQuery.

I have the following json object which is sent from the server side: enter image description here

And the following jQuery and Ajax code in my HTML template:

<script type=text/javascript>
  $(function() {
    $('a#get-more').bind('click', function() {
      $.getJSON($SCRIPT_ROOT + '/_get_the_data_from_serverside', {
      }, function(data) {
        $("#more").append(data.stories[0].storycontent);
      });
      return false;
    });
  });
</script>
<span id=more></span>
<a href="#" id=get-more></a>

But it doesn't work as you can see, the data from the json object like "storycontent" is not being appended to the HTML.

Any ideas?

11
  • You need id="more", you are missing the " " Commented Jun 29, 2016 at 9:09
  • You have to copy the div or something which is you are repeating in Python. Just replace at the HTML of that with your json object values. Commented Jun 29, 2016 at 9:09
  • @JesperHøjer Done that and it didn't change anything. Commented Jun 29, 2016 at 9:10
  • You don't get any error in the console, if you press F12? Commented Jun 29, 2016 at 9:11
  • @SunilPachlangia How exactly? Unfortunately I don't know jQuery functionality well. Commented Jun 29, 2016 at 9:11

1 Answer 1

1

The path for the json should be data[0].storycontent. Like so:

<script type=text/javascript>
  $(function() {
    $('a#get-more').bind('click', function() {
      $.getJSON($SCRIPT_ROOT + '/_get_the_data_from_serverside', {
      }, function(data) {
        $("#more").append(data[0].storycontent);
      });
      return false;
    });
  });
</script>
<span id="more"></span>
<a href="#" id=get-more></a>
Sign up to request clarification or add additional context in comments.

Comments

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.