Node.js 前端仅接收参数为;";
Node.js 前端仅接收参数为;";,node.js,reactjs,express,parameters,fetch,Node.js,Reactjs,Express,Parameters,Fetch,每当我请求时,不管我传递什么,它都会在服务器上显示{id:'t'} app.get("/get_test?:id", (req, res) => { console.log(req.params.id); res.send(req.params); }) // client side code fetch("/get_test?" + props.id) .then(res => res.json()) .then(
每当我请求时,不管我传递什么,它都会在服务器上显示{id:'t'}
app.get("/get_test?:id", (req, res) => {
console.log(req.params.id);
res.send(req.params);
})
// client side code
fetch("/get_test?" + props.id)
.then(res => res.json())
.then(data => {console.log("Data from server: ",data)})
.catch(err => console.log(err))
如果要获取查询参数,只需使用query(而不是params) 或使用参数,但url将不同
app.get("/get_test/:id", (req, res) => {
console.log(req.params.id);
res.send(req.params);
})
如果要获取查询参数,只需使用query(而不是params) 或使用参数,但url将不同
app.get("/get_test/:id", (req, res) => {
console.log(req.params.id);
res.send(req.params);
})
您没有在url中指定查询参数名称
fetch("/get_test?id=" + props.id) // here
.then(res => res.json())
.then(data => {console.log("Data from server: ",data)})
.catch(err => console.log(err))
而且,在服务器端,您不应该像这样形成get处理程序,因为您混合了路径参数和查询参数
app.get("/get_test", (req, res) => { // here do not use the ?:id
console.log(req.query.id);
res.send(req.query);
})
您没有在url中指定查询参数名称
fetch("/get_test?id=" + props.id) // here
.then(res => res.json())
.then(data => {console.log("Data from server: ",data)})
.catch(err => console.log(err))
而且,在服务器端,您不应该像这样形成get处理程序,因为您混合了路径参数和查询参数
app.get("/get_test", (req, res) => { // here do not use the ?:id
console.log(req.query.id);
res.send(req.query);
})