Retrieving JSON data by key through ajax does not show the expected output

I'm currently working with JSON data in the form of an array, and I'm facing some issues. Here's how the data looks:

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5a69c9b96908790b59485879c99db97c8">[email protected]</a>",
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d380bbb2bdbdb293beb6bfbaa0a0b2fda7a5">[email protected]</a>",
  }
]

To fetch and display this data using AJAX, I'm using a function where I parse the JSON object from a string. I first initialize an XMLHttpRequest object:

let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState === 4 && this.status === 200) {
        var jsonObj = JSON.parse(xhttp.responseText);

Next, I add a new element to the document:

var table='<tr><th>Name</th><th>Email</th></tr>';
        table += '<tr><td>' +
            jsonObj["name"] +
            '</td><td>' +
            jsonObj["email"] +
            '</td></tr>'
        document.getElementById('demo').innerHTML = table;
    }
};

Despite my efforts, the browser isn't displaying the values in the table field as expected - instead, it shows undefined. I've tried changing to JSON.stringify(), but the issue persists. Can someone help me identify what might be wrong here?

Answer №1

To iterate through the array, as mentioned by @Lee Taylor, utilizing a loop is crucial.

for (let element of jsonArray) {
    output += "<div>" + element.title + "</div>"
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing my Ajax application to fail on my local machine?

Even though my XAMPP server is activated, the Ajax scripts are not functioning properly. I followed all the instructions from a book and checked the codes as well as the database connections, but unfortunately, it did not work. I am struggling to identify ...

The malfunction of the AJAX toolkit SliderExtender is triggered by the ASP.NET timer

In my current setup, I have implemented 2 update panels with UpdateMode set to "Conditional". The first update panel consists of: 2 AJAX toolkit SliderExtender components 2 corresponding Text Boxes Within the first update panel, I have configured the T ...

Invoking a function within a functional component from a React element

Let's imagine a scenario where we have a Child component that is a functional component and contains a function called a(): export default function child({ ... }) { ... function a() { ... } ... } Now, let's introduce a parent ...

Steps for enabling a feature flag via API in specific environments

Within my project, I am working with three separate environments and I am looking to activate a feature flag only for a specific environment. Is it feasible to toggle an unleash feature flag using the API for just the development environment? The code snip ...

Issue encountered: Document not being saved by Mongoose, error message received: "MongoClient connection must be established"

Currently, I am attempting to establish a connection with MongoDb using mongoose. Below is the code snippet that I have implemented for this purpose: controller.js const conn = mongoose.createConnection(db, { useNewUrlParser: true, ...

“What is the process of setting a referenced object to null?”

Here is an example of the code I'm working with: ngOnInit{ let p1 : Person = {}; console.log(p1); //Object { } this.setNull<Person>(p1); console.log(p1); //Object { } } private setNull<T>(obj : T){ obj = null; } My objective is to ...

Can you explain the distinction between sockets and proxy passing in nginx compared to uwsgi?

My latest project setup involves flask, uwsgi, and nginx. The backend solely serves json data through an API, while the client side takes care of rendering. Typically, my Nginx configuration looks like this: My usual setup (with basic proxy passing): se ...

The Chevron icon is not pointing downwards even though it has already gone upwards

I want to toggle a chevron icon, but nothing seems to be happening. $("span:last").removeClass("glyphicon-chevron-down").addClass("glyphicon-chevron-up"); When I add this code below the slideToggle function without an if-else section, the icon changes to ...

What is the best way to switch from using the three.js JSONLoader to the ObjectLoader?

I successfully loaded multiple files using the code below for three.js JSONLoader: <!DOCTYPE html> <html> <head> <title>Rotating Sphere</title> <meta charset="utf-8"> <!-- https://cdnjs.com/libra ...

What is the process to insert a record into a table by triggering an AJAX call upon clicking "save

I'm looking to dynamically update a table with data from a database using AJAX. Specifically, I want the table to reflect any new records added by the user without having to refresh the entire page. Below is my JavaScript code snippet for handling thi ...

Page could not be refreshed using ajax

Having trouble updating a page with a set of HTML links from another page every 5 seconds. It seems to only work when I manually refresh the browser. Any assistance would be greatly appreciated. HTML PAGE <div id="sto"></div> <script> ...

Unraveling nested JSON Response in Struts 2 jQuery <sj:select> tag - A step-by-step guide

Currently, I am developing a web application that utilizes Struts2 and its jQuery Plugin. The <sj:select> tag in my code below works perfectly when the JSON response is not nested: <s:url id="remoteurl" namespace="/XYZ" action= ...

Determining the length and angle of a shadow using X and Y coordinates

I'm currently tackling the task of extracting data from a file generated by a software program that has the ability to add a shadow effect to text. The user interface allows you to specify an angle, length, and radius for this shadow. https://i.stack ...

Easily find exactly what you're looking for with a straightforward

My ES documents (Tweepy JSON) have the following structure: { "_source": { "id": 792477813014224900, "metadata": { "iso_language_code": "en", "result_type": "recent" }, "retweeted": false, "retweet_count": 330, "user": { "id": 149250899, "listed_count": 0 ...

Incorporating additional ES6 modules during the development process

As I build a React component, I find that it relies on an ES6 component that I'm currently developing. Since I created the latter first, what is the typical method to include it during development as I work on the second component? If the dependency w ...

Vue js is throwing an error message that says "Reading 'push' property of undefined is not possible"

I've encountered an issue while trying to navigate to other routes. The error I'm receiving is: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'push') at eval (JoinRoom.vue?bd9d:74:1) This is how I pu ...

What is the best way to send a value through an AJAX request?

My ajax update function is not working in the code below. How can I solve this issue? The edit method in my code is functioning properly. For example, the value is being passed in this code snippet: var name = row.find(".ContactPersonName").find("span"). ...

Generating a JSON Object by combining elements from multiple arrays

I need assistance in creating a single Json object from two arrays using JavaScript or jQuery. The data is stored in the database in the format shown below: clob_field_1: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 etc etc ... clob_field_2: 8106.23, 7856.49, 8009.15, ...

Is there a way to showcase a resultset using async await in JavaScript?

I need help formatting the resultset from my query into a json list. Currently, only the first record is being displayed. How can I use async await to return multiple records? https://i.sstatic.net/rrPPW.png I am looking to achieve this output https://i. ...

Issues with resetting AngularJS form---"The AngularJS form

I have been putting in a lot of effort to make this work. Despite my knowledge about child scopes, prototypal inheritance, and the use of dot notation for the model, I am facing an issue with resetting the form. You can access the hosted form here. The rel ...