1
我试图通过验证的用户登录注册获取细节反应原生JSON数据。 但它不工作。我是新来的本地人,所以任何人都可以说我在做什么错。获取用于验证用户登录
Index.js
onPressLogin()
{
this.setState({
isLoading: true,
error: '',
});
dismissKeyboard();
fetch('http://10.0.2.2/movies/signup.php' ,{
method: 'GET',
headers: {
'Accept' : 'application/json' ,
'Content-Type': 'application/json'
},
body : JSON.stringify({
contact : this.state.contact,
password: this.state.password
})
}).then((response) =>
{
return response.json();
}).then((response) => {
if(response.json){
const routeStack = this.props.navigator.getCurrentRoutes();
this.props.navigator.jumpTo(routeStack[3]);
AsyncStorage.multiSet([
[ 'contact' , response.contact],
]);
} else {
alert("Invalid login");
}
});
}
我使用PHP作为后端。
Signup.php
<?php
include 'db.php';
$check_json = file_get_contents('php://input');
echo $obj1=json_decode($check_json);
$contact=$obj1 ->{"contact"};
$pass=$obj1 ->{"password"};
$prep = $mysqli->prepare("select contact,password from signup where
contact=? and password=?");
$prep->bind_param("ss",$contact,$pass);
$prep->execute();
echo $result1=$prep->get_result();
$count= $result1->num_rows();
if($count>0){
echo '1';
}
else{
echo'0';
}
?>
更改方法:'GET'到'method:'POST'? – Val
**从来没有明文存储密码!**。只存储密码哈希!使用PHP的['password_hash()'](http://php.net/manual/en/function.password-hash.php)和['password_verify()'](http://php.net/manual/en /function.password-verify.php)。如果您运行的PHP版本低于5.5(我希望不是),那么可以使用[password_compat库](https://github.com/ircmaxell/password_compat)来获得相同的功能。 –