我正在尝试发出 GET 请求,该请求将触发来自 mySQL 数据库的 SELECT 查询。但是,我需要动态请求,因为要查询的数据取决于用户输入。以下是我根据执行 POST 请求的方式得出的结论:
*当选择相应的组件时,handlePress 函数就会被触发
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);
}
})
})
关于javascript - mySql/Express GET 请求到 React Native 应用程序中的动态 SELECT 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44540160/