我正在使用此代码传递userName变量...
return fetch('http://creat1vedesign.com/userTabs4.php', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}, {
body: JSON.stringify({
userName: 'carolf'
}),
...此php脚本...
<?php
include 'DBConfig.php';
$con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$userName = $obj['userName'];
$sql = "select * from Users where userName = '$userName'";
if ($result = mysqli_query($con,$sql)) {
while($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>
...以检索特定条件的数据。但是或者React Native没有发送数据或者php脚本没有接收到数据或者语法不正确或者我在做其他错误的事情:(
有什么想法吗?
答案 0 :(得分:0)
您可以通过在参数“?”之后添加参数来将参数传递给获取URL中的PHP脚本。像这样:
fetch("http://creat1vedesign.com/userTabs4.php?userName=carolf",{
method:'POST',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then( (response) => {
return response.json() })
.then( (json) => {
console.log(json)
});
只需确保在您的php脚本中包含$ _GET:
<?php
$userName= $_GET["userName"];
include 'DBConfig.php';
$con = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$sql = "select * from Users where userName = '$userName'";
if ($result = mysqli_query($con,$sql)) {
while($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>