在React Native中从PHP获取数据

时间:2019-08-14 19:09:37

标签: react-native

我想通过Api在本机响应中从PHP获取结果。

如果我运行此代码,则控制台中将出现一些错误: 状态:200,正常:是,但是状态文本未定义。但我不知道为什么会出现此错误。

反应本机代码

 myFunction = () => {
    fetch('http://{ip}/api/index.php?email=user@gmail.com&password=123', {
      method: 'GET',
      headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
}
   })
   .then((response) => console.log(response))
   .catch((error) => {
      console.error(error);
   });
  }

PHP代码

<?php
header('Content-Type: application/json');
$conn = mysqli_connect("localhost","root","","api");

$email = $_GET["email"];
$password = $_GET["password"];

$query = mysqli_query($conn,"select * from tbl_users where email = '".$email."' AND password = '".$password."'");

$result = mysqli_fetch_array($query);


if(count($result) > 0)
{
    $response = array("status" => 1,"message" => "Successfull Logged In");

}
else
{
    $response = array("status" => 0,"message" => "Invalid Username & Password");
}

echo json_encode($response);
?>

1 个答案:

答案 0 :(得分:0)

response.json()添加到您的API回调

myFunction = () => {
    fetch('http://{ip}/api/index.php?email=user@gmail.com&password=123', {
        method: 'GET',
        headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
    }
})
    .then((response) => response.json())
    .then((response) => console.log(response))
    .catch((error) => {
        console.error(error);
    });
}