H
H
heIIfire2017-09-29 00:09:34
Node.js
heIIfire, 2017-09-29 00:09:34

How to get JSON from client in ExpressJS?

Here is the AJAX request

const data = { 
                         name: "vasya", 
                         age: 32 
            };
            let xhr = new XMLHttpRequest();
            xhr.open("POST", "/sign-in");
            xhr.setRequestHeader("Content-type", "application/json");
            xhr.onreadystatechange = function() {//Call a function when the state changes.
                if(xhr.readyState == 4 && xhr.status == 200) {
                   console.log(xhr.responseText);
                }
            };
            xhr.send(JSON.stringify(data));

On the server, request.body is empty - {}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question