【发布时间】:2020-05-14 10:02:41
【问题描述】:
我最近开始用 express 框架学习 node.js,偶然发现了一个问题。
我正在尝试一个简单的查询搜索 webapp,我想传递一段数据(一个名为 all 的布尔值)
从前端纯javascript通过ajax到服务器端,但当前代码
我现在写的,好像我的数据没有传到服务器端,
我种的3个console.log全部返回{}。
我在这里缺少什么概念?我究竟做错了什么?
如果需要任何其他信息,请告诉我,谢谢。
前端js
window.onload=function(){
console.log("window loaded");
const xhr = new XMLHttpRequest();
xhr.open("POST","http://127.0.0.1:3000/search", true);
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status==200){
// code later to be implemented
}
}
let searchConditions = new SearchConditions(true);
console.log('data='+JSON.stringify(searchConditions));
xhr.send('data='+JSON.stringify(searchConditions));
}
class SearchConditions{
constructor(all) {
this.all = all;
}
}
后端 node.js
// only partial code
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/search', (req, res) => {
console.log(req.body);
console.log(req.query);
console.log(req.params);
});
我的浏览器日志
我的后端日志
【问题讨论】:
标签: javascript node.js ajax express