ORDS, JavaScript, the Fetch API, and HTML

I found JavaScript and HTML code here and here and “remixed” it to work with one of my sample ORDS APIs. Here is the result:

the-html-in-browser, chris hoina, db tools, ords, oracle database, javascript, html, ords api, oracle rest api
ORDS + JavaScript + Fetch API + HTML

Impressive, no? Care to try it out? Read on friend!

References

I’ll front load with all the necessary stuff. That way, you can bounce if you don’t feel like reading. You’ll get the gist if you follow along with what I’ve provided.

Much of what I learned came from the MDN Web Docs site. I would get acquainted with the following pieces of code (or at least have them handy) since they heavily influenced me (a.k.a. plagiarized).

MDN Web Docs

I either used or referenced these files in my version of the code. They are all available in the two links I mentioned above, but I’m adding them here for convenience (in case you need to leave or want to review while on this page).

ORDS code

Here are a few things to point out:

  1. In line 16 of my index.html code, I referenced the JavaScript code (script.js) separately. This approach achieves the same effect as embedding the JavaScript directly into the HTML file (as seen in the MDN’s version of the index.html file).
  2. The script.js contains the Fetch API and the JavaScript concept of “promises.” The following were super helpful for me. Maybe the will be for you too:
  3. The JSON file contains an example of what an ORDS GET request response looks like (if viewing in the browser). The structure is nearly identical if you compare it to the MDN JSON file.
    • This means you can take their HTML and JavaScript code and populate it with an ORDS endpoint and [subsequent] response data (i.e., the stuff you see in this localhost.json file).

Live Server

I’m also using the Live Server extension for VS Code. If you don’t have it, you’ll need it to run the code I’ve provided. You can download it from the VS Code Marketplace here.

mentioning-live-server-for-this-exercise, chris hoina, db tools, ords, oracle database, javascript, html, ords api, oracle rest api
You’ll want Live Server for this one!

How I met your Mothra 👾

Where to start? From the beginning, right? What you see below are two JSON files. On the left, from ORDS. On the right, from the MDN Web Docs sample code (direct link to that file).

Comparing JSÒN

comparing-ords-and-mdn-json-files, chris hoina, db tools, ords, oracle database, javascript, html, ords api, oracle rest api
ORDS on the left, MDN on the right.

They are nearly identical. They are both a JSON object {} comprised of key: value pairs, where the first key’s value is an array []. In both files, this array has more objects {}. And each of those objects has its own key: value pairs…marone 🤌🏼!

I mention all this because this makes the existing code easy to work with. Which you’ll see shortly.

Comparing JavaScript

Next is the JavaScript code; I’ll compare both my version and the MDN Web Docs version.

comparing-javascript-versions-with-ords-apis, chris hoina, db tools, ords, oracle database, javascript, html, ords api, oracle rest api
ORDS on the left; can you spot the differences?

You’ll notice that a lot of the code is quite similar. I kept it this way, so I wouldn’t unintentionally break anything. The main differences in my code are the:

  1. const ordsAPI on Line 1 (as opposed to referencing a JSON file).
  2. Naming conventions in lines 14-27.
  3. listItem.append(); on line 29 is heavily remixed (I did this so I could create individual lines for each entry).
  4. Templating in my code (i.e., wherever you see the little ``` marks; they allow you to embed text directly into the HTML) I use A LOT more of it!

About the ORDS JSON Object

If you were to navigate to your ORDS endpoint, it would look like the images below. I’m including them for a couple of reasons:

  1. You can see those key: value pairs in a different presentation.
  2. These images help connect what is coming through in that GET request and what you see in the JavaScript code.
an-example-of-ords-response-in-browser, chris hoina, db tools, ords, oracle database, javascript, html, ords api, oracle rest api
The items key with its value (an array).
the-raw-ords-response-in-browser, chris hoina, db tools, ords, oracle database, javascript, html, ords api, oracle rest api
Remember the other key: value pairs, too!

Reviewing the HTML

Assuming you’ve started up Live Server (along with setting up your environment to mimic my own), you’ll immediately see this beauty of a web page. This image alone doesn’t tell a complete story, though.

the-html-in-browser, chris hoina, db tools, ords, oracle database, javascript, html, ords api, oracle rest api
Review line 29 in the JavaScript code; it’ll help to “connect the dots.”

However, when you open up the developer tools in your browser, you’ll see what is happening under the covers.

  1. Live Server starts up, sees the index.html file, and “serves” it up.
  2. In that HTML file is a reference to script.js; the JavaScript is run.
  3. The JavaScript composes a list and then appends all the data you see here (on screen):
html-with-developer-tools-to-inspect-html-and-javascript, chris hoina, db tools, ords, oracle database, javascript, html, ords api, oracle rest api
With developer tools open, you can see the HTML. This HTML should look similar to lines 12-27 of the JavaScript code.

Summary

After writing this up, I’m realizing this clearly needs to be a video. But if you get it, great! Otherwise, stay tuned!

There isn’t anything ground-breaking here. I’m highlighting an example of manipulating existing ORDS JSON objects (with the Fetch API) because I hadn’t seen anything quite like what I am presenting here.

Also, the web page that I’m showing is very, very basic. I’m neither a UX nor UI designer, so this is what you get, folks!

The main point is that the ORDS APIs are effortless to work with if you have a fundamental understanding of manipulating JSON objects using JavaScript. They are no different than what you see out in the wild.

Some follow-up

I want to take this and add some React to it. And I’d also like to add authentication (Basic, OAuth 2.0, and Java Web Tokens). But baby steps.

Okay, that’s all for now, folks, Sayonara!

Follow

And don’t forget to follow, like, subscribe, share, taunt, troll, or stalk me!

Leave a Comment