提问者:小点点

前端仅接收参数为T


服务器侧br>每当我请求时,不管我传递什么,它都会在服务器上显示{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))

共2个答案

匿名用户

如果您想要获得查询参数,只需使用查询(而不是参数)

app.get("/get_test?:id", (req, res) => {
    console.log(req.query.id);
    res.send(req.query);
})

或使用参数,但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);
})