Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax call on document load not working

Hello I want to do an ajax call when a document is loaded. here is the code

  script(src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js')  
  script.
      $.ajax({ url: '/labeling/page_refresh', 
              type: 'GET', 
              success: function(data){ console.log(data) }, 
              error: function(error) { console.log(error)} 
              });

this is the server side

router.get('/labeling/page_refresh', function (req, res, next) {
  console.log("refreshed!")
  db_label.findLabels((err, labels) => {
    if (err) {
      return res.sendStatus(500)
    }
    res.send(labels)
  })
})

labels console.logs this

[ { _id: 5a73e852316509492f5e0067, label: 'moo: 00:00:00' },
  { _id: 5a73e85a316509492f5e0068, label: 'poo: 00:00:00' } ]

client side

  h4(class='div-class' id='labels')

I am getting back no errors but the data is my own pug file in html format!

like image 541
Ricky Avatar asked Dec 05 '25 16:12

Ricky


1 Answers

$(document).ready(function(){ 

   $.get( "/labeling/refresh", function( data ){ 
      // success callback
      console.log(data);
   }, function (error){
      //error callback
      console.log(error);
   }); 

}); 
like image 113
Khurshid Ansari Avatar answered Dec 08 '25 04:12

Khurshid Ansari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!