0

I need to hit a get route in my js file. I am using nodejs and express on server side. The route to which i need to make a get request app.get('/book', function(){});

$('#myModal').on('hidden.bs.modal', function(){
    // note sure what to fill in here
});
pkja
  • 25
  • 6
  • 1
    So basically your question is "how to do a get request" and it is not related at all to the bootstrap modal. Right? – Dekel Aug 30 '17 at 22:18
  • 1
    Possible duplicate of [Ajax tutorial for post and get](https://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get) – Dekel Aug 30 '17 at 22:19
  • i need a normal get request to happen and it should execute the code in the route and render the page. Please help. – pkja Aug 30 '17 at 23:02

1 Answers1

1

Use the jQuery's built in get method:

$('#myModal').on('hidden.bs.modal', function(){

    $.get("/books", { id: 123 }, function(response) {
         // You can do whatever you want with the response here, like...
         $(".container").html(response);
    });

    // The response variable is async, so you wont be able to use it outside that scope

});

Then, in your back end, you will have a function that receives the request for that endpoint, something like:

function(request) {

   var bookID = request.id;

   // Fetch book data from your database
   var bookData = YourModelMethod.getBook(bookID);

   return "<div>" + bookData.title + "</div>";

}

You can find more details in jQuery documentation: https://api.jquery.com/jquery.get/

  • but this is kind of an ajax request. I need to hit the "/books" route and execute the code inside the route. And also i will append a query string - "/books?id=kjdhdjh". Hope my question is clear. – pkja Aug 30 '17 at 22:57
  • I don't understand very well what are you trying to achieve, do you want to append the id before or after you make the request? If you are trying to get the code that renders an specific book, then you can pass the id as an argument when you do the get request, I'll update the example – Jorge Álvarez Aug 31 '17 at 17:25