【发布时间】:2020-04-23 15:51:43
【问题描述】:
我正在使用快速服务器,并希望使用 XMLHttpRequest 将数据从前端发送到它。 后端是这样的
const app = express()
const port = 3000
var bodyParser = require('body-parser')
// To parse json
app.use(bodyParser.json());
app.get('/',function(req,res){
res.send('Hello World!')
})
app.post('/',function(req,res){
res.send('This is a post request')
console.log(req.body)
})
app.listen(port, function(){
console.log(`Example app listening at http://localhost:${port}`)
})
前端是这样的
console.log(JSON.stringify(dataObject))
let xhr = new XMLHttpRequest();
xhr.open("POST","http://localhost:3000/");
xhr.send(JSON.stringify(dataObject))
xhr.onload = function(){
alert(xhr.response);
}
}
我在快速控制台上得到空括号输出。 请帮忙
【问题讨论】:
标签: node.js express xmlhttprequest