-1

So, I am making an app that will allow me to input some data on the client side, whether phone or another desktop running in the same network.

I have a Node.js server, which answers my requests, but I can't actually "read" them in the server side.

this is my code for the server

var http = require("http");

var parseString = require('xml2js').parseString;

var express = require('express')
var app = express();
var port = process.env.PORT || 8081;


var request = require('request');


app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});


// start the server
app.listen(port);
console.log('Server started! At http://localhost:' + port);


// routes will go here
app.use('/static', express.static('.'));

var bodyParser = require('body-parser');
app.use('/browse', bodyParser.json()); // support json encoded bodies
app.use('/browse', bodyParser.urlencoded({ extended: true })); // support encoded bodies


app.get('/', function(req, res, next) {
  // Handle the get for this route
});

app.post('/', function(req, res, next) {
 // Handle the post for this route
});


// POST http://localhost:8081/browse
app.get('/browse', function(req, res) {
  var browseRequest = req.body.browseRequest;
  console.dir("browseRequest: ");
  console.dir(browseRequest);

    res.send(JSON.stringify("response"));

});

(do I have redundant stuff here?)

And this is the part I am using for my html page

// Create the XHR object.
    function createCORSRequest(method, url) {

      var xhr = new XMLHttpRequest();
      if ("withCredentials" in xhr) {
        // XHR for Chrome/Firefox/Opera/Safari.
        xhr.open(method, url, true);
      } else if (typeof XDomainRequest != "undefined") {
        // XDomainRequest for IE.
        xhr = new XDomainRequest();
        xhr.open(method, url);
      } else {
        // CORS not supported.
        xhr = null;
      }
      return xhr;
    }


    // Make the actual CORS request.
    function makeCorsRequest() {
      // This is a sample server that supports CORS.
        var url = "http://192.168.0.100:8081/browse"
      var xhr = createCORSRequest('GET', url);
      if (!xhr) {
        alert('CORS not supported');
        return;
      }

        //xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

          var req = { "browseRequest": {
                "name": "Aaron",
                "type": "Consumer",
                "age": 21

          }};
          xhr.send(JSON.stringify(req));

    }

I get the string sent by the server on my client, but I can't access the browserequest in the server because req.body is coming blank.

What am I missing here? Thanks !

Kevin B
  • 94,570
  • 16
  • 163
  • 180
seugnimod
  • 64
  • 1
  • 9
  • you failed to tell the server what you were sending it. (the commented out part) – Kevin B Oct 10 '16 at 15:20
  • I commented it before because I was getting CORS error. Even with it uncommented I still have the body empty. Any tip ? – seugnimod Oct 10 '16 at 15:36

1 Answers1

-1

GET parameters are (mainly) bodyless. (Read this)

You can fetch parameters with req.params or req.query

But in your ajax call you have to queryString your data.

Community
  • 1
  • 1
Steeve Pitis
  • 4,283
  • 1
  • 21
  • 24