1

I am learning how to load json data into .js file. I have created a employee.json file. I saved my js and json file and on the desktop. What I trying to do is to put all the id in json files into an array in the js. I do not know what could be wrong. Hope someone could help me out. Thank you in advance.

<!DOCTYPE html>
<html>
    <head>
      <meta charset="UTF-8">
      <title>JSON with jQuery</title>
    </head>

<body>
  <p id="demo"></p>
  <h1><h2>
  <script src = "<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script>
      var res = [];
      $.ajax({
          url: 'employee.json',
          dataType: 'json',
          type: 'get',
          cache: false,
          success: function(data) {
              $(data.people).each(function(index, value) {
                 res.push(value.id);
              });
          }
      });

      document.getElementById("demo").innerHTML = res.toString();
  </script>

</body>
</html>

{
"person": [
    {
        "id" : 1,
        "firstName" : "Lokesh"
    },
    {
        "id" : 2,
        "firstName" : "bryant"
    }
    {
        "id" : 3,
        "firstName" : "kobe"
    }
]
}
5
  • In the json you have person object but you used people object in the each iterator.Any Reason??? Commented Nov 28, 2016 at 6:06
  • 1
    data.people or data.person Commented Nov 28, 2016 at 6:06
  • setup a localhost and do the same. Commented Nov 28, 2016 at 6:07
  • 1
    comma is missing in your json file after id 2 Commented Nov 28, 2016 at 6:10
  • I changed it but it still does not work Commented Nov 28, 2016 at 8:06

3 Answers 3

4
  • Error 1: Typing error. <script src = "<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>. You mistyped the src of the script, accidentally adding another another <script> start tag.
  • Error 2: Misplaced statement. document.getElementById("demo").innerHTML = res.toString(); should be placed in the success callback function, so it will be executed only after the server responds. If it executes prematurely, res will still be [].
  • Error 3: type: 'GET' should be method: 'GET', according to the docs (though 'GET' is default so you don't need to explicitly write this).

Use this:

<p id="demo"></p>
<h1><h2>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script>
      var res = [];
      $.ajax({
          url: 'employee.json',
          dataType: 'json',
          method: 'get',
          cache: false,
          success: function(data) {
              $(data.people).each(function(index, value) {
                 res.push(value.id);
              });
              document.getElementById("demo").innerHTML = res.toString();
          }
      });


  </script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank u Kyle for the response. I changed in the way as your suggestion and I change person to people which is another typo, but it is still not right. Did you miss something here?
I'm not sure what is going on. Perhaps you should use '/employee.json' (begin with slash to make path absolute). Also, I don't know where you are hosting this. A cross-origin request could also be the problem. The Same Origin Policy also has special rules for /localhost/ and file:// URLs (which I forgot), so if you are hosting from localhost there, you should look into those restrictions.
0

You can't use the local json to read. it gives cross origin request failure. so deploy both the files (html and json) into a we server and execute. or place the json data onto some web url(http://somesite/myjson) and then request that url and see.

Comments

-2

First of all, the JSON shouldn't be existed as in physical "file". It has to be generated by a backend language / web service etc. The JSON tags inside a manually created "file" have high chance of data invalidity upon parsing.

Solution

Use a Web Service to generate valid JSON output. And from Javascript end, use:

JSON.stringify( data );

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.