为什么我能';使用react native无法从PHP获取数据?
我有一个使用react native和php(后端使用sqlServer数据)开发的移动应用程序,但最近,我想运行我的应用程序,但遇到了一个问题: 当我单击一个按钮,通常必须显示我在php文件中的信息时,我什么也没有得到(即使没有显示空警报,我也不知道问题是否出在fetch方法上!!) 这是我的代码:为什么我能';使用react native无法从PHP获取数据?,php,react-native,Php,React Native,我有一个使用react native和php(后端使用sqlServer数据)开发的移动应用程序,但最近,我想运行我的应用程序,但遇到了一个问题: 当我单击一个按钮,通常必须显示我在php文件中的信息时,我什么也没有得到(即使没有显示空警报,我也不知道问题是否出在fetch方法上!!) 这是我的代码: <TouchableOpacity onPress={this.userRegister}><Text>Connexion</Text></Touchab
<TouchableOpacity onPress={this.userRegister}><Text>Connexion</Text></TouchableOpacity>
userRegister = () =>{
fetch('http://192.168.1.21/recup.php',{
method:'post',
header:{
'Accept':'application/json',
'Content-type' :'application/json'
},
body:JSON.stringify({})})
.then((responseData)=>responseData.text())
.then((responseJson)=>{
alert(responseJson)
})
.catch((error)=>{console.error(error);})
}
连接
用户注册=()=>{
取('http://192.168.1.21/recup.php',{
方法:'post',
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
},
正文:JSON.stringify({})
.然后((responseData)=>responseData.text())
.然后((responseJson)=>{
警报(应答)
})
.catch((错误)=>{console.error(错误);})
}
因为我的php文件工作得很好(当我在浏览器中使用时,我得到了“数据匹配”)
如果有人有同样的问题,或者知道我为什么会遇到这个问题,可能是cors,你做了什么调试?我不理解你的评论,请你澄清一下控制台。错误正在触发,但除非你在你的移动应用程序模拟器等中查看调试工具,否则你不会看到它。将其更改为
alert(error)
或正确处理/调试它,您很可能会看到它抛出一些cors(跨源资源共享)错误,然后您可以通过谷歌搜索解决方案。因此,除非您的手机/应用程序位于同一LAN网络192.168.1.
将其更改为alert(error),但不会得到任何结果
<?php
$serverName="DESKTOP-T5SLVUB\SQL2008R2";
$connectionInfo=array("Database"=>"","UID"=>"sa","PWD"=>"");
$conn=sqlsrv_connect($serverName,$connectionInfo);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
echo('data matched')
?>