Skip to content Skip to sidebar Skip to footer

How To Make An XMLHttpRequest That Inserts JSON Data Into HTML?

Hey I'm trying to make and XMLHttpRequest that gets a JSON , and afterwards inserts an specific field of it into HTML. For example insert just the data of Angelo his name , lastnam

Solution 1:

On the line document.getElementById("name").innerHTML=myArr[0]; you're indexing into an array, but your JSON file contains an object. You'd have to either change it to myArr.angelo or change your JSON file to contain an array.


Solution 2:

The way you have formatted your json is incorrect. Design your json like this and you should be able to access the way you are accessing now.

[
  {
    "Name": "Angelo",
    "LastName": "Giuseppe",
    "Age": 19
  },
  {
    "Name": "Hugo",
    "LastName": "Dóminguez",
    "Age": 25
  },
  {
    "Name": "Edgar",
    "LastName": "Villareal",
    "Age": 35
  },
  {
    "Name": "Salvador",
    "LastName": "Ramirez",
    "Age": 20
  }
]

Post a Comment for "How To Make An XMLHttpRequest That Inserts JSON Data Into HTML?"