Ryan Cameron
Ryan Cameron

Reputation: 371

Make an ajax call from nodejs server side

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.

Upvotes: 0

Views: 1463

Answers (2)

Alexander
Alexander

Reputation: 104

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.

Upvotes: 1

John
John

Reputation: 21

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/

Upvotes: 1

Related Questions