
[英]Express Server cannot GET request from the React Native app
[英]mySql/Express GET request to a dynamic SELECT query in a React Native application
我正在尝试发出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);
}
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.