2017-07-05 4 views
0

Ich möchte einige json Daten von meinem Client-Seite auf Server-Seite von node.js schicken, hier ich meine Server-Seite haben:wie JSON-Daten von HTML-Seite senden Server node.js

var express = require('express'); 
 
var bodyParser = require('body-parser'); 
 
var app = express(); 
 

 
app.use(bodyParser.json()); 
 

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

 
app.listen(8081); 
 
console.log('listening on 8081');

Client-Seite:

var name ='someName'; 
 
var xhttp = new XMLHttpRequest(); 
 
xhttp.onreadystatechange = function(){ 
 
if(this.readyState== 4 && this.status == 200){ 
 
    console.log(this.responseText); 
 
\t } 
 
}; 
 
xhttp.setRequestHeader({'Content-Type': 'application/json'}); 
 
xhttp.open('POST', 'http://localhost:8081', true); 
 
xhttp.send(JSON.stringify({'name' : name}));

Ich habe das Ergebnis als null Json {}.

HINWEIS: Ich möchte nicht über die Übermittlung eines Formulars, ich möchte nur JSON-Daten von HTML-Datei an node.js-Datei senden.

+0

bekam ich die Antwort auf HTML-Seite als empfangene Anfrage, sondern eine Null-json auf Server-Seite. –

Antwort

Verwandte Themen