0

Skip this part, it's me rambling: Not duplicate as it didn't solve my problem and he is trying to make an ajax call as a query search.

Not a duplicate of How to make external HTTP requests with Node.js - I am not asking how to make an external http request but an ajax call.

No, I can not just use request, I don't want to get into why but ajax is the only method that'll work. I have already tested it on the client side, I just have to figure out how to implement it on the server side.


ACTUAL QUESTION

$.ajax({
     url: "https://example/api/card/",
     dataType: "JSONP",
     type: "GET",
     jsonpCallback: "callback",
     data: {
         id: var1,
         scid: var2,
         cid: var34
     },
     success: function() { console.log("res", res) }
});

I need to somehow make the above ajax call from the server side of my nodejs application.


I HAVE to use ajax as the api I am sending it to only accepts a specific kind of request that I am trying to mimic.

Ryan Cameron
  • 371
  • 1
  • 4
  • 14
  • 1
    Please include the server side code you're referring to and specify which part of that code is your question about. – Nima Aug 17 '18 at 19:08
  • I don't got the problem here, I only see that you are trying to send DATA in a GET operation and this would't work. – BrTkCa Aug 17 '18 at 19:27
  • Possible duplicate of [How to make external HTTP requests with Node.js](https://stackoverflow.com/questions/7967037/how-to-make-external-http-requests-with-node-js) – pretzelhammer Aug 17 '18 at 19:32

2 Answers2

1

Could you not just use request?

request.get({
    headers: {'Content-Type' : 'application/json'},
    url: "https://example/api/card/",,
    json: true
}, function(error, response, body){
.....

https://stackabuse.com/the-node-js-request-module/

John
  • 36
  • 3
1

There is a bunch of NPM packages that can help you to solve it, for example: https://www.npmjs.com/package/node-fetch

Usage:

fetch('http://httpbin.org/post', { 
    method: 'POST',
    body:    JSON.stringify(body),
    headers: { 'Content-Type': 'application/json' },
})

Hope that helps.

Alexander
  • 104
  • 2