0

My site stores strings in a json file and uses javascript to retrive value to HTML. Here's simplified version of the function.

HTML:

<html>
<head>
    <script src="script.js"></script>
</head>

<body>
  This is a demonstration. The author is:
    <div>
      <div class="author"></div>
    </div>
</body>
</html>

Javascript:

request = new XMLHttpRequest();
request.open('GET', 'data.json', true);

request.onload = function () {
   obj = JSON.parse(request.responseText);
   obj.forEach(function (d) {
        var el = document.createElement('p');
        el.textContent = d.author;
        document.getElementsByClassName('author')[0].appendChild(el);
    });
};

request.send();

Json:

{
  "author": "John Doe"
}

Implementation: http://embed.plnkr.co/Ixrz9R2KVLXpOqa7mwyU

The code doesn't return author name in the HTML. Is there anyway to do this without using jQuery if possible.

4
  • Is your json an array of objects? Commented Jan 21, 2017 at 8:17
  • In my actual office json, it is an array. But this code is still not working. Commented Jan 21, 2017 at 8:25
  • 2
    Instead of editing the question title, accept the answer that provided a solution to you. You can do this by clicking on the tick icon to the left of an answer. StackOverflow has a built-in system to tag solved/answered questions automatically. Commented Jan 21, 2017 at 8:44
  • In the same vein, do not add the answer to the question. Commented Jan 21, 2017 at 13:57

1 Answer 1

1

The reason

You have to load scripts like this:

<script src="script.js"></script>

The solution

Remove

<link href="script.js">

, and put

<script src="script.js"></script>

onto right before the </body>.

I recommend you put your script onto right before the </body> in order to let it be executed after the DOM elements are loaded.


The code

data.json

[
    {"author": "K."},
    {"author": "Hello world"},
    {"author": "John Doe"}
]

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="author" content="K.">

    <title>Demo</title>
  </head>

  <body>
    <p>This is a demonstration. The author is:</p>
    <div class="author"></div>

    <script src="script.js"></script>
  </body>
</html>

script.js

const request = new XMLHttpRequest; // `()` isn't needed.
request.open("GET", "data.json", true);
request.addEventListener("load", _ => { // Arrow function
    const parsed = JSON.parse(request.responseText);
    parsed.forEach(({author}) => { // Destructuring, Arrow function, ...
        const authorElement = document.createElement("P");
        authorElement.textContent = author;
        document.querySelector(".author").appendChild(authorElement);
    });
});
request.send();
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.