简体   繁体   中英

How would I display a Json data from my Http get request file onto a blank html page

I created a js file that allow me to retrieve/output the data from an api, how would i display this file to a html page.

was using node.js to compile my data through the terminal

If you guys know a bunch a method , please link me it. thank you.

var realmStatus = " http://api.dol.gov/V1/Statistics/OES/OE_OCCUPATION/?KEY=1ce7650d-b131-4fb7-91b3-b7761efc8cd4 ";

var http = require("http");
var options = {
        host: 'api.dol.gov',
        path: realmStatus,
        type: 'GET',
        dataType: 'json',
        accept: 'application/json'
        };


 console.log("Start");
var x = http.request(options,function(res){
    console.log("Connected");
    res.on('data',function(data){

        data =JSON.stringify(data.toString());
        data =JSON.parse(data.toString());

       console.log(data.toString()+"\n");
    });
}});
x.end();

if you're using Node try installing the Pug (formerly Jade) package. Then you can pretty easily pass the JSON to a pug page that renders HTML. https://pugjs.org/api/getting-started.html

A little but more detail would be helpful, but I think what you need is simply to make a html page and include the js file as a script? If you want to constantly display updated data from a request make a function that updates a div on the html page. Somthing like,

HTML: index.html

...
<head><script src="/path/to/js/file">..</head>
<div id="data"></div>

JS: file.js

var callback = function(data){
 if (data) document.getElementById('data').innerhtml = JSON.stringify(data);
}
var api_call = ajax_request(callback);

If it is an included JS file in the head, I think @Daniel's answer above should work for you.

If it's a call to API, you can consider using jQuery AJAX call and display the response data in a Div tag Basic Implementation

HTML

<div id="div1"></div>

JS

$.ajax({url: "domain.com/api", success: function(result){
        $("#div1").html(result);
    }});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM