1

My purpose is to send a JSON object from the client-side to the server. So taking into account the following details of the client:

   <script>
        var onClickFunct = function() {
            trial1.send({
                "test": $("#input_test").val(),
            });
        }
    </script>

Where input_test is the name of my tag in the html page. And where trial1 is implemented with the following code:

var trial1 = {
    //notifica al server una nuova registrazione
    send: function(data){
        $.getJSON('/tst',data);
    }
}

My Server can't see the object; infact if I print req.params it shows me "undefined".

app.get('/tst',function(req){
    console.log(req.params);
});

My index.html reuire the following script <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

While the server require only express

pittuzzo
  • 493
  • 8
  • 29
  • Can you upload complete server code ,does it has const bodyParser = require('body-parser'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); – Sumeet Kumar Yadav Dec 27 '16 at 17:10

2 Answers2

1

Try changing:

app.get('/tst',function(req){
    console.log(req.params);
});

to:

app.get('/tst',function(req){
    console.log(req.query);
});

The req.params is for parameters in your routes, like:

app.get('/test/:name', function (req, res) {
    console.log(req.params);
});

When you access /test/abc then the req.params.name will be "abc".

The $.getJSON() sends the data as query parameters, see:

And for the query parameters you use res.query.

If it was a POST request and you wanted to access parameters passed in the request body then you would user req.body (but you would need to use a bodyParser middleware).

Remember to use GET only for reading operations, where in the query parameters you specify what you want to get or in what form, what order etc.

If you pass data to store in a database or something like that then use POST or PUT for that and pass the data in the request body (which can be JSON).

rsp
  • 107,747
  • 29
  • 201
  • 177
1
$ npm install --save body-parser

and then:

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

your endpoint should look like this:

app.post('/tst',function(req){

    console.log(req.body);
});

then use Jquery Post:

$.post('/tst',data);

body parser install example taken from here:

How to retrieve POST query parameters?

hope it helps :)

Community
  • 1
  • 1
Samy
  • 125
  • 3
  • 6
  • I found it very usefull; I tried and it is working too. Just the difference is that now the communication is crypted, correct? – pittuzzo Dec 27 '16 at 23:02
  • not exactly encrypted, the only real technical difference (please correct this post if I'm wrong) is that GET has a much shorter limit to the query string. In practice, GET is meant for when fetching something from the server. A GET call should not cause side effects on the server. POST is when you intend to send something on the server and have it do something with it. – Samy Dec 28 '16 at 21:25