2

I have a code where i am trying to get some value from a drop down menu using a get request. However, when i click on submit, the request object is empty. i did use the body parser and bodyParser.json and declared them before the routes, however the request object is still empty:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var EventHubClient = require('azure-event-hubs').Client;

const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
console.log('');
};


app.get('/result', function(req, res){
  res.sendFile(__dirname + '/result.html');
});

app.get('/index', function(req,res) {
  res.sendFile(__dirname + '/index.html');
});

app.get('/getJson', function (req, res) {
    console.log(req.body);
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

here is the index.html file:

<form id="tableForm" action="/getJson" method="get">
    <select class="selectpicker" data-style="btn-info" name="selectpicker">
        <optgroup label="Select Table">
            <option name="" value="0">Select table</option>
            <option name="table1" value="1">Table 1</option>
            <option name="table2" value="2">Table 2</option>
            <option name="table3" value="3">Table 3</option>
        </optgroup>
    </select>
    <input type="submit" />
</form>
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Neo
  • 89
  • 1
  • 9

3 Answers3

0

Try post method in your form in index.html

lakshay gaur
  • 39
  • 1
  • 9
0

Firstly, you need to change the form method to POST in your index.html page:

<form id="tableForm" action="/getJson" method="get">
                                               ^^^

Now, since the method is POST in your HTML page, you have to make changes in this code snippet too:

app.get('/getJson', function (req, res) {
    console.log(req.body);
});

The changes will be reflected as:

app.post('/getJson', function (req, res) {
    console.log(req.body);
    res.end();    //You must add this line as well.
});

response

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
  • Thankyou. This manages to solve the issue. Can you explain why did you use POST instead of get for form? – Neo Jul 31 '17 at 07:12
  • You must read the [basics](https://www.w3schools.com/tags/ref_httpmethods.asp) first :) – Raktim Biswas Jul 31 '17 at 07:14
  • On the other note, how can i redirect from the /getJson to another page, for example result.html? – Neo Jul 31 '17 at 07:17
0

You should use POST method to pass data.

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var EventHubClient = require('azure-event-hubs').Client;

const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
console.log('');
};


app.get('/result', function(req, res){
  res.sendFile(__dirname + '/result.html');
});

app.get('/index', function(req,res) {
  res.sendFile(__dirname + '/index.html');
});

app.post('/getJson', function (req, res) {
    console.log(req.body);
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

index.html

<form id="tableForm" action="/getJson" method="post">
    <select class="selectpicker" data-style="btn-info" name="selectpicker">
        <optgroup label="Select Table">
            <option name="" value="0">Select table</option>
            <option name="table1" value="1">Table 1</option>
            <option name="table2" value="2">Table 2</option>
            <option name="table3" value="3">Table 3</option>
        </optgroup>
    </select>
    <input type="submit" />
</form>
Vishnu
  • 11,614
  • 6
  • 51
  • 90