0

python:

import json
posts = [
{'author':'JL Rowling','title':'Harry Potter'},
{'author':'JRR Tolkien','title':'Lord of the Rings'},
]
encoded_posts = json.dumps(posts)

javascript:

let obj = {{ encoded_posts }};

JS error logged in console:

Uncaught SyntaxError: Unexpected token ';'

Here are the page according to the CONSOLE. It seems to be losing(?) the {{encoded_posts}}.

<!DOCTYPE html>
<html>
  <head>

      <title>Flask Blog - About</title>

  </head>
  <body>
    <h1>About page!</h1>
    <script>
      let text = document.getElementsByTagName('h1')[0];
      text.addEventListener('click',function(){
        text.style.color = 'red';
      });

      let obj = ;
      let jsonPosts = JSON.parse();
      console.log(jsonPosts);

    </script>
  </body>
</html>

What's happening?

1
  • 2
    You have a typo in your script tag. let obj = ; Commented May 25, 2020 at 20:19

1 Answer 1

1

Have you tried doing:

let obj = JSON.parse('{{ encoded_posts }}');
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.