Wenn cURL oder Postman verwenden, alles wiepost-Methode Fehler NodeJS req.body ist Renditen {}
curl -d 'username=Johny Sample&title=My First Post&description=We want some help cleaning up after the hurricane&postID=Johny Sample_1' http://localhost:3000/NewPost
Anforderer Ergebnisse
{"posterID":"Johny Sample","title":"My First Post","description":"We want some help cleaning up after the hurricane"}
Server Ergebnisse
req.body == {
\t \t \t \t username: 'Johny Sample',
\t \t \t \t title: 'My First Post',
\t \t \t \t description: 'We want some help cleaning up after the hurricane',
\t \t \t \t postID: 'Johny Sample_1'
\t \t \t }
funktioniert nicht von Browser
**
function gatherData()
\t {
\t \t var retData \t ='title='+el("title").value+'';
\t \t retData \t +='&description='+el('description').value+'';
\t \t postID \t \t \t = 'Johny Sample_1';
\t \t return \t \t \t retData;
\t }
\t function save() {
\t \t var xhttp = new XMLHttpRequest();
\t \t \t xhttp.onreadystatechange = function() {
\t \t \t if (xhttp.readyState == 4 && xhttp.status == 200) {
\t \t \t \t a = xhttp.responseText;
\t \t \t }
\t \t };
\t \t xhttp.open("POST", "http://localhost:3000/NewPost", true);
\t \t var sendData = gatherData();
\t \t xhttp.send(sendData);
\t }
**
Anforderer Ergebnisse
null
Server Ergebnisse
{} {}
Server Side-Code
\t app.post('/NewPost', function (req, res) {
\t \t console.log(req.body);
\t \t var post \t = {};
\t \t post.posterID \t = req.body.username;
\t \t post.title \t = req.body.title;
\t \t post.description= req.body.description;
\t \t post.ID \t \t = req.body.ID;
\t \t console.log(post);
\t \t res.send(post);
\t })
Haben Sie 'checked req.query' statt' req.body' wenn die URL durch Browser schlagen? –