-1

I made an API call with Nodejs and rendered the data in my ejs file. The JSON data that I parsed in my ejs file has a unique ID. I need to pass this ID back to the server so I can make another call to the API with this unique ID.

I know how to request data from a form with nodejs but how do I request a single object like an ID?

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Rocky Singh
  • 25
  • 1
  • 5
  • Why do you need to pass the data back to your server? Why can't you use the ID *before* you pass the rest of the data that it came with to your EJS? – Quentin Aug 12 '18 at 19:04
  • "I know how to request data from a form with nodejs but how do I request a single object like an ID?" — There are lots of ways. You could put it in a form. – Quentin Aug 12 '18 at 19:04
  • @Quentin I can't use the ID before passing it to the ejs file. I have to wait until a user clicks on one of the objects (JSON objects that came back from the API call) and then I want to render another page with that unique object (ID). – Rocky Singh Aug 12 '18 at 19:09
  • OK, so why not use a form? – Quentin Aug 12 '18 at 19:10
  • @Quentin I know I can do this with jquery but I would rather keep on the server side so it stays dynamic. – Rocky Singh Aug 12 '18 at 19:11
  • I never mentioned jQuery, or any other client-side JavaScript. Why not use a form? – Quentin Aug 12 '18 at 19:11
  • @Quentin okay, So if I have a javascript object how do I put it in a form so I can request on the server side? – Rocky Singh Aug 12 '18 at 19:13

1 Answers1

0

To send data to your server you can create an AJAX post request like below, you will have to setup the endpoint on the server. You can also use query strings or parameters in the url. What is the difference between URL parameters and query strings?

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://yourdomain/sendId", //Server End point
  "method": "POST",
  "headers": {
    "Content-Type": "application/json",
    "Cache-Control": "no-cache",
  },
  "processData": false,
  "data": {"id": myID } //put your id value here
}

$.ajax(settings).done(function (response) {
  console.log(response);
});