Storing the information received from an API as an HTML element

My HTML file contains JavaScript, along with a URL that displays data retrieved from an AWS lambda API call via AWS API Gateway. The page initially appears blank and the data is structured like this:

[
{"user": "bob", "groups": ["bobsGroup"], "policies": ["bobsPolicy", "anotherPolicy"["Policy3"]]},
{"user": "sal", "groups": ["salsGroup"], "policies": ["salssPolicy", "anotherPolicy"["Policy3"]]}
]

I am looking to extract the data from the API response and store it within an HTML paragraph element. However, I suspect my approach is incorrect as I have been attempting to achieve this by embedding the following code snippet within my script tags:

var myjson;
$.getJSON("https://myapi.execute-api.us-east-2.amazonaws.com/myapi//myapiresource", function(json){
    myjson = json;
});
document.getElementById("demo").innerHTML = myjson;

The identifier demo refers to a paragraph tag in my HTML document.

Here is the complete code snippet:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">


  </head>
  <body>


  <div id = "demo"></div>




    <script>

        var myjson;
$.getJSON("https://myapi.execute-api.us-east-2.amazonaws.com/myapi//myapiresource", function(json){ 
  document.getElementById("demo").innerHTML = json;
});


    </script>


  </body>
</html>

Answer №1

Make sure to include

document.getElementById("demo").innerHTML
inside the ajax success callback function...

var myjson;
$.getJSON("https://myapi.execute-api.us-east-2.amazonaws.com/myapi//myapiresource", function(json){
  myjson = json; 
  document.getElementById("demo").innerHTML = myjson;
});

Simplified Code Version

$.getJSON("https://myapi.execute-api.us-east-2.amazonaws.com/myapi//myapiresource", function(json){ 
  document.getElementById("demo").innerHTML = json;
});

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

Vue error: Uncaught promise rejection - RangeError: The computed value update has exceeded the maximum call stack size

My computed code snippet: computed: { display: { get() { return this.display }, set(newValue) { this.display = newValue } } }, Attempting to update the computed value from a function in ...

An issue encountered with res.download() following res.render() in Node.js

Just started working with Node JS and ran into an issue: Error: Can't set headers after they are sent. I've checked my code, and the problem seems to be related to res.download(); Is there a way to display the view without using res.render()? ...

Failure to append an object to the array occurs when submitting an HTML form

I am facing an issue with a form that is supposed to add input values to an array as objects when submitted. However, every time I submit the form, the console briefly shows that there is an array with one object, only for it to disappear. Below is the f ...

What is the best way to organize code into separate files while utilizing a module that needs to be included in every file?

In this particular scenario, I am utilizing a headless browser with Puppeteer Chrome and MongoDB. Take a look at the following code snippet: var browser = await puppeteer.launch() var page = await browser.newPage() var db = await MongoClient.connect(" ...

Custom Homepage with Integrated Google Web Search and Autocomplete Feature

Is there a way to incorporate the standard Google web search with autocomplete into my own webpage, without using a custom search engine like www.google.com? I have been unable to find any examples or guides on how to accomplish this. All the resources see ...

The ZIP file downloaded from NodeJS is corrupted and cannot be opened

Currently, I am utilizing the following code to create a MySQL dump in memory, then zip that SQL file with a password from memory and save it to the hard drive so it can be streamed to the client... /* DUMP - database */ var mysqld ...

Can someone explain why my table in Laravel is only showing the third set of data and not the others?

I'm facing an issue with displaying all of the data in my table named taglineImageCarousel. The problem lies in the slide_ID column, where it is being populated with data from slide-01, slide-02, and slide-03. Below is the code snippet: taglineImage ...

Can you help me understand how to ensure the CSS translate feature lands in a specific div, no matter its initial position?

I'm facing a roadblock in my journey to create a card game. The issue arises at the final stage of the Translate function implementation. In this game, the player is dealt 30 cards (I've simplified it to four for ease of programming), and upon cl ...

How can one break down enum values in typescript?

I've defined an enum in TypeScript as shown below: export enum XMPPElementName { state = "state", presence = "presence", iq = "iq", unreadCount = "uc", otherUserUnreadCount = "ouc", sequenc ...

Incorporating an external script within a Next.js application

I've been having trouble incorporating an external JavaScript source into my Next.js application and I keep encountering the following error: Failed to execute 'write' on 'Document': It isn't possible to write into a documen ...

Implementing interactive dropdown menus to trigger specific actions

I have modified some code I found in a tutorial on creating hoverable dropdowns from W3. Instead of the default behavior where clicking on a link takes you to another page, I want to pass a value to a function when a user clicks. Below is a snippet of the ...

Translating Encryption from Javascript to Ruby

I have an application which utilizes HTML5 caching to enable offline functionality. When the app is offline, information is stored using JavaScript in localStorage and then transmitted to the server once online connectivity is restored. I am interested in ...

Retrieve the width of an element once the browser has finalized its dimensions

I am facing an issue with centering a pop-up box perfectly within the window using CSS properties. The code for the pop-up box styling is as follows: #pop_up { position: fixed; display: inline-block; border: 2px solid green; box-shadow: 0p ...

Tips for deleting extraneous cells in a table without altering the placement of the other cells and refraining from employing CSS

I'm struggling with understanding how to merge cells in a table. Can someone explain the rules for merging table cells to me? I want to remove certain cells from the table without using CSS, I just want to make it look clean. <table border="1" ...

Learn the process of seamlessly uploading various document formats, videos, and previewing documents with Angular software

I am having trouble viewing uploaded files in the carousel. While I can see video and image files, other document formats are not displaying. Can someone please recommend a solution to enable viewing all types of documents as well? mydata = [] onSelect ...

Tips for adjusting the default selection in a second dropdown menu

I have a dilemma with my two dropdown lists, "optionone" and "optiontwo". I am trying to alter the default selected value from "option value=3>3" to option value=3 selected>3 when the user selects 2 from the first dropdown list ("optionone"). <script&g ...

Using Javascript or Jquery, you can submit a form without the need for a button

I'm attempting to submit a form without using a button by invoking a JavaScript function and processing the form with JQUERY/PHP. My goal is for the form to be submitted silently on the backend without causing the page to reload. However, I keep encou ...

Tweepy in Python experiencing intermittent errors while streaming

I have implemented this code to extract the content from live streams using the 'text' identifier to capture tweet texts. Although it is mostly functioning well, occasionally I encounter a "Not Working" message due to an exception in the code whe ...

Strong tags are not functioning properly within paragraph tags in Firefox

It has come to my attention that when I insert <strong> into a <p>, the text does not appear bold. This issue seems to only occur in Firefox, as Chrome and Safari display it correctly. However, when I place <strong> inside a <li>, e ...

Retrieving a particular value from a JSON array using SQL syntax

Imagine having a table column filled with person data stored in a JSON array format, complete with categories and names. With SQL, it is possible to retrieve the information for a specific element in the array effortlessly: SELECT JSON_VALUE('{ " ...