Javascript mySql/Express获取对React本机应用程序中的动态选择查询的请求
我正在尝试发出一个GET请求,它将从mySQL数据库触发一个SELECT查询。但是,我需要请求是动态的,因为查询的数据取决于用户输入。以下是我根据如何执行POST请求得出的结论: *选择相应组件时,将触发handlePress功能Javascript mySql/Express获取对React本机应用程序中的动态选择查询的请求,javascript,mysql,reactjs,react-native,restful-architecture,Javascript,Mysql,Reactjs,React Native,Restful Architecture,我正在尝试发出一个GET请求,它将从mySQL数据库触发一个SELECT查询。但是,我需要请求是动态的,因为查询的数据取决于用户输入。以下是我根据如何执行POST请求得出的结论: *选择相应组件时,将触发handlePress功能 handlePress = (inputId) => { fetch('http://127.0.0.1:3000/testData', { "method": "GET", body: JSON.stringify({ id:
handlePress = (inputId) => {
fetch('http://127.0.0.1:3000/testData', {
"method": "GET",
body: JSON.stringify({
id: inputId
})
})
.then((response) => response.json())
.then((responseData) => {
this.setState({newData: responseData})
})
}
app.get('/testData', function (req, res) {
connection.query('select * from ticket_data where id = ' + req.body.id, function(error, results, fields) {
if(error) {
console.log('Error in GET / query')
} else {
res.send(results);
}
})
})
好吧,我想起来了。您必须使用req.query;我这样做的方式只对POST请求有效。如果其他人遇到同样的问题,以下是我的解决方案:
handlePress = (inputId) => {
fetch('http://127.0.0.1:3000/testData?id=' + inputID, {
"method": "GET"
})
.then((response) => response.json())
.then((responseData) => {
this.setState({newData: responseData})
})
}
app.get('/testData', function (req, res) {
connection.query('select * from ticket_data where id= ' + req.query.id, function(error, results, fields) {
if(error) {
console.log('Error in GET / query')
} else {
res.send(results);
}
})
})