Masnad Nihit
Masnad Nihit

Reputation: 1996

get json information from node js server with ajax

I am tryin get ajax call my node js server and get a json body back to the client.

Here is my ajax call code.

self.information = function() {
      $.ajax({
          type: 'GET',
          url: 'http://localhost:3000/getIOT',
          contentType: 'application/json; charset=utf-8'
      })
      .done(function(result) {
        console.log(result);
      })
      .fail(function(xhr, status, error) {
          console.log(error);
      })
      .always(function(data){
      });
  }
}

Here is the node js code.

app.get('/getIOT', function (req, res , err) {
            request({
            'auth': {
                'user': 'masnad',
                'pass': 'whatstodaysrate',
                'sendImmediately': true
            },
            url: 'https://get.com/getAuroraRate?Amount=1000',
            method: 'GET',
        }, function (error, request, body) {
            console.log(body);
            return response.end(JSON.stringify(body));
        })
});

Error I am getting is net::ERR_CONNECTION_REFUSED and the body doesnt get returned to the ajax call.

/Users/nihit/Documents/node/multiple-js/server/server.js:36
            return response.end(JSON.stringify(body));
                   ^

ReferenceError: response is not defined
    at Request._callback (/Users/nihit/Documents/node/multiple-js/server/server.js:36:20)
    at Request.self.callback (/Users/nihit/Documents/node/multiple-js/node_modules/request/request.js:186:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:192:7)
    at Request.<anonymous> (/Users/nihit/Documents/node/multiple-js/node_modules/request/request.js:1081:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:189:7)
    at IncomingMessage.<anonymous> (/Users/nihit/Documents/node/multiple-js/node_modules/request/request.js:1001:12)
    at Object.onceWrapper (events.js:291:19)
    at emitNone (events.js:91:20)

Not sure what I am doing wrong.

Upvotes: 3

Views: 2638

Answers (1)

technophobia
technophobia

Reputation: 2629

You have a typo: response should be res

app.get('/getIOT', function (req, res , err) { // <-- response is 'res'
        request({
        'auth': {
            'user': 'masnad',
            'pass': 'whatstodaysrate',
            'sendImmediately': true
        },
        url: 'https://get.com/getAuroraRate?Amount=1000',
        method: 'GET',
    }, function (error, request, body) {
        console.log(body);
        return res.end(JSON.stringify(body)); // <-- res
    })
});

Addendum:

Based on your comment below, here's some clean up that can be done to your request:

$.ajax({
    type: 'GET',
    url: 'http://localhost:3000/getIOT',
    dataType: 'json',                              // <-- add this
    contentType: 'application/json; charset=utf-8' // <-- remove this
})
.done(function(result) {
    console.log(result);
})
.fail(function(xhr, status, error) {
    console.log(error);
})
.always(function(data){
});

Explanation:

  • dataType tells jQuery to parse the returned data as JSON.
  • contentType tells the server that the data you're sending it is JSON, but you're not sending a payload, so you don't need this on your GET request.

Upvotes: 2

Related Questions