Dieses Problem kann nicht durch einfache Suche gelöst werden. Wenn jemand das deutlicher machen kann?Express.js POST leer req.body
Im Client habe ich versucht, Objekt an xhr.send (Obj) anhängen. Im Moment zu Formdata-Objekt zu anhängen versuchen, führen die gleiche, während ... Client-Code:
var xhr = new XMLHttpRequest()
xhr.open("post", "/api/test", true)
var formData = new FormData()
formData.append("hi", "hello")
xhr.send(formData)
xhr.onreadystatechange = function() {
if (this.readyState != 4)
return
if (this.status != 200) return console.error(this.status + this.statusText)
else console.log(this.responseText)
}
Und auf Back-End versuche ich req.body diese Weise zu erhalten:
const express = require('express'),
app = express(),
http = require('http'),
path = require('path'),
bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static('public'))
app.get('/', function(req, res) {
res.sendFile(path.resolve("/public/index.html"))
})
app.post('/api/test', (req, res) => {
console.log(req.body)
res.end()
})
Ich verstehe nicht warum, aber jedes Mal, wenn ich nur leere Objekte ausgedruckt bekomme. Ich habe mehrere Male checked Front-End sendet Objekte.
Schätzen Sie jede Hilfe.
So einfach =)) vielen Dank. Ich habe ungefähr eine Stunde gespielt)) – Lazyexpert
Kein Problem :) Gern geschehen – jaumard