Search code examples
javascriptangularjsnode.jsxmlhttprequest

Cannot get angularjs http request message data at node.js


The client side code snippet:

$http({
  method: 'GET',
  url: '/admin?operation=getAdminInfo',
  data: {data: 'test'}
}).then(function(rsp){...});

The server side code snippet:

console.log('req = ' + req);
console.log('req.header = ' + JSON.stringify(req.header));
console.log('req.query = ' + JSON.stringify(req.query));
console.log('req.body = ' + JSON.stringify(req.body));
console.log('req.cookies = ' + JSON.stringify(req.cookies));
console.log('req.params = ' + JSON.stringify(req.params));
console.log('req.xhr = ' + JSON.stringify(req.xhr));

The output in the server console:

req = [object Object]
req.header = undefined
req.query = {"operation":"getAdminInfo"}
req.body = {}
req.cookies = {"token":"eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzeXN0ZW0iLCJpc3MiOiJodHRwczovL3d3dy5uZXRhb3RlYy5jb20iLCJpYXQiOjE1MTYwNjM1Njk1MDQsInVzZXJfcm9sZSI6MSwidXNlcm5hbWUiOiJzeXN0ZW0iLCJleHAiOjE1MTYwNjM1NzY3MDR9.K2Rh4WTNtkhP2gxWLnGW7846zMKaW-WKEF2hykJmvn3zNmGyIDprrnswzqqrpmAvNTP8gylnCn3b7K_gRJ-WOif7MnQYiG4mu8fNGW-hCUQeTyqzUgsP9sIXzm_A3pWFlW8eL88Dydugf9JhhDeB18QUJV-i4zT6bCfE3stY1QXJZzNsKd8HRl22n78XCb5XRxdiEnmhqF6sNb9h40jKzcd6Ny0KX6uEe1SE54OYbp_BcR4Lr69C6tcFNgwtiJusFEymnMcbkqSst_GUztiObPeYZIwrfEUMEobUsxvx0v2zUQp9EJDF-MyaGid5kJwyv_ittFFKRChBSllI3luNXA"}
req.params = {}
req.xhr = false

How can I extract/get the data {data: 'test'} at node.js? AngularJS version is 1.6.6; Node.js version is v8.1.4; app.js at node.js: enter link description here


Solution

  • Change the method from Get to Post, since you are sending an object to the server.

    $http({
      method: 'POST',
      url: '/admin?operation=getAdminInfo',
      data: {data: 'test'}
    }).then(function(rsp){...});