Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda