0

I'm stuck for quite some time, tried a lot but didn't get the solution.

So, in my app, on hitting the success button I'm showing a success page, and in the same class, I want to post JSON data to a specific URL.

const options = {
    hostname: 'localhost',
    port: 3500,
    path: '/responseSent',
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Accept-Charset': 'utf-8'
    }
};

Below is my post function:

router.post('/', function(req, res, next) {
    // Below is the data I need to send to const options (some other URL other than success.ejs)


   // Need some code here to send the data (let dataToSend = req.app.locals;) to http://localhost:3500/responseSent

   // below is the page I want to show the success animation only (success.ejs locally in project).
    res.render('success');
});

I need to render the page at success.ejs, and at the same time need to send the JSON to the other URL. Kindly help.

Thanks in advance :)

  • 1
    It's really unclear what you are asking. Are you trying to make two HTTP requests from the browser simultaneously? Do you want to make an HTTP request from Node.js in response to it *recieving* an HTTP request from the browser? – Quentin Feb 27 '20 at 09:31
  • Hi Quentin, 1). I'm rendering on success.ejs where some success animation is shown. 2). And, also I want to send the JSON data to some other url. So, I'm stuck in the second part. – Kumar.Ashish Feb 27 '20 at 11:33
  • I suggested two possibilities in my comment. You managed to reply without confirming either of them. – Quentin Feb 27 '20 at 11:41
  • Hey Quentin, 1). I'm making two calls. One is one clicking button the success page is rendering (success.ejs at my case). Secondly, at the same time I want to transfer the JSON data at const options = { hostname: 'localhost', port: 3500, path: '/someURLToSendData', method: 'POST', headers: { 'Accept': 'application/json', 'Accept-Charset': 'utf-8' } }; – Kumar.Ashish Feb 27 '20 at 11:48
  • And you still haven't clarified which of the two cases I proposed was the one you wanted to do. – Quentin Feb 27 '20 at 11:49
  • Is this a duplicate of https://stackoverflow.com/questions/6158933/how-is-an-http-post-request-made-in-node-js ? – Quentin Feb 27 '20 at 11:54
  • Hi Quentim, I just want to make two calls on same post request.i.e. one is internally directed (opening a ejs page), the other is to send the data to a specific url. ( I hope I'm able to communicate the issue). – Kumar.Ashish Feb 27 '20 at 11:56
  • No. You're failing utterly. You are still just repeating the same thing about "making two calls" without being clear about where either of them are coming from. I essentially gave you a "pick one of these two options" in my first comment and you haven't. – Quentin Feb 27 '20 at 11:57
  • Okay, so I've one call coming from previous controller, (so the date is in req.app.locals; ) which is JSON, now I want to send this data to some URL and while opening success.ejs at the same time. (success.ejs is to show some animation, not sending any data here). – Kumar.Ashish Feb 27 '20 at 12:01
  • Look at my first comment. Copy/paste one of the two options. Write "This one" next to it. Or tell me both are wrong. – Quentin Feb 27 '20 at 12:02
  • Are you trying to make two HTTP requests from the browser simultaneously? I'll go with this one. – Kumar.Ashish Feb 27 '20 at 12:08
  • So what's the problem (it's hard to tell because your question doesn't have an [mcve])? Do you need an introductory Ajax tutorial? – Quentin Feb 27 '20 at 12:12
  • Hope I've updated the question for more clarity. – Kumar.Ashish Feb 27 '20 at 14:30

0 Answers0