I
I
its2easyy2016-02-18 00:10:36
JavaScript
its2easyy, 2016-02-18 00:10:36

How to transfer data to express and receive back?

Script on the client

$(document).ready(function() {
            $('#submit').click(function() {
                // вместо params будут данные с формы
                var params = JSON.stringify({"name": "John", "date": "45"})
                $.post("/123", params,
                function(data){
                 alert(data);
                 });
            });
        });

On server
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

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

Express framework, you need to transfer data from the form to the server and then get a response from it in the form of json. Console.log outputs { '{"name":"John","date":"45"}': '' } and the browser alert is [object Object].
How to transfer data correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
timfcsm, 2016-02-18
@timfcsm

because the alert brings the object to the string , also output to the console in the browser and you will see that everything comes up correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question