Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
How do i get to dynamically refresh JSON data in an EJS tag

I'm currently working on an node/express app that shows several informations about the ISS. I'm using an API shared by the NASA returning JSON data.

I want the data to be updated dynamically on my page using EJS, without the need to refresh it. let's say every 1000ms.

Here's my actual code to get the ISS's current Longitude and Latitude:

Server side:

app.get("/iss", function(req, res){
   var url = "http://api.open-notify.org/iss-now.json";
   request(url, function(err, response, body){
       if (!err && response.statusCode == 200){
          var data = JSON.parse(body);
          res.render("ISS", {data: data});
       }
   });
});

Client side :

<div class="ui main text inverted container segment">
    <div class="ui huge header">Where is the ISS ?</div>
    <p>Longitude : <%= data["iss_position"].longitude %> </p>
    <p>Latitude : <%= data["iss_position"].latitude %> </p>
</div>

I've heard about Socket.io but i don't know if it would be optimized for my need. Also, can i get it to work by using setInterval()function ? If yes, can you help me with that ?

Thanks for your help.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Sample api call for iss endpoint

app.get("/iss", function(req, res){
       var url = "http://api.open-notify.org/iss-now.json";
       request(url, function(err, response, body){
           if (!err && response.statusCode == 200){
              var data = JSON.parse(body);
              res.send(data);
           }
       });
    });

Client side code-

<div class="ui main text inverted container segment">
    <div class="ui huge header">Where is the ISS ?</div>
    <p>Longitude :<span id="longitude"> <%= data["iss_position"].longitude %> </span></p>
    <p>Latitude : <%= data["iss_position"].latitude %> </p>
</div>

Assuming you are using jQuery

<script>
 $.ajax({
   "url":<base_url>+"/iss",
   "success":function(data){
      $("#longitude").html(data.iss_position.longitude);
   },
   "error":function(error){
   //handle error here
   }
})
</script>

I want the data to be updated dynamically on my page using EJS, without the need to refresh it. let's say every 1000ms.

You may need to call the Ajax function using setInterval to update send the request every 1000ms

Code for setInterval

<script>

setInterval(function(){

     $.ajax({
       "url":<base_url>+"/iss",
       "success":function(data){
          $("#longitude").html(data.iss_position.longitude);
       },
       "error":function(error){
       //handle error here
       }
    })

}, 1000);

</script>

$(document).ready(function() {

  setInterval(function() {
    $.ajax({
      "url": "http://api.open-notify.org/iss-now.json",

      "success": function(data) {
        $("#longitude").html(data.iss_position.longitude);
      },
      "error": function(error) {
        console.log(error);
      }
    })
  }, 1000);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="longitude">
</span>

You can check the snippet above and see the longitude updating.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!