0

Here is my code.

back end code (express)

router.get('/principal/leave/approval/:id',function(req,res){
 console.log('requested');
 var approval;
  if(req.body.approve){
   approval=true;
  }else{
   approval=false;
  }
  leaveDB.update({_id:req.params.id},{approved:approval},function(err){
   if(err){
    console.log(err);
   }else{
    res.send('success');
  }
 });
});

here is the front end

<input type='button' value="Approve" id="Approve" class="btn<%=details._id%>"/>
<input type='button' value="Disapprove" id="Disapprove" class="btn<%=details._id%>"/>
</form>
<script>
  $(document).ready(function(){
    $('.btn').on('click',function(){
     var id=this.className.slice(4);
     $.ajax({
      type:'GET',
      url:'/principal/leave/approval/'+id,
      data:{approval:this.id},
      success:function(data){
       alert('success');
      }
    });
   });

  });
</script>

when I run this I dont even see the "requested" printed in the console. Is there any fault in my code or should I use something else to make a request like this? (I am using ejs as the view engine)

dilanSachi
  • 388
  • 4
  • 11
  • 1
    How is the `router` being setup for `.use()` by your Express `app`? If you check your browser's dev tools, does its Console or Network monitor show any issues with the request? Or, does jQuery itself report an `error`? – Side note: [GET requests shouldn't be expected to have a `req.body`](https://stackoverflow.com/questions/978061/http-get-with-request-body). – Jonathan Lonowski Apr 05 '18 at 18:25
  • 1
    Since this is a GET request, what happens when you just go to the url from a browser? – Luca Kiebel Apr 05 '18 at 18:27
  • Uncaught TypeError: $.ajax is not a function at HTMLInputElement. (5ac61a68d89acf03f0379779?approve=Approve:88) at HTMLInputElement.dispatch (jquery-3.2.1.slim.min.js:3) at HTMLInputElement.q.handle (jquery-3.2.1.slim.min.js:3) – dilanSachi Apr 05 '18 at 18:28
  • Also a GET seems like the wrong action to take. – epascarello Apr 05 '18 at 18:28
  • @DilanSachinthaNayanajith so it has nothing to do with the Ajax request.... You are using jQuery slim which does not have ajax built in. – epascarello Apr 05 '18 at 18:29
  • @Luca it says 'success' – dilanSachi Apr 05 '18 at 18:30
  • @epascarello so what should I do? – dilanSachi Apr 05 '18 at 18:31
  • 1
    Use a version of jQuery that has $.ajax support – epascarello Apr 05 '18 at 18:31
  • ok...thanx everyone for helping me...I will try that – dilanSachi Apr 05 '18 at 18:32

1 Answers1

0

finally I understood the fault in my code from the help of the contributors. The fault was not with the code, but since I was using a version of jQuery that didn't support ajax, browser didn't understood the ajax request. Instead it showed Uncaught TypeError: $.ajax is not a function. So if anyone has the same problem better try your program with a <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> in your html layout.

dilanSachi
  • 388
  • 4
  • 11