我已从网站“ http://carlofontanos.com/user-login-with-wordpress-using-react-native/”中遵循了此代码
我已经这样完成了我的Login.js
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
Keyboard
} from 'react-native';
import { AsyncStorage } from 'react-native';
import {Actions} from 'react-native-router-flux';
import { StackNavigator } from 'react-navigation';
export default class LoginForm extends Component<{}> {
constructor(props){
super(props)
this.state={
userEmail:'',
userPassword:'',
validating: false
}
}
login = () =>{
this.state.validating = true;
const {userEmail,userPassword} = this.state;
let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
if(userEmail==""){
//alert("Please enter Email address");
this.setState({email:'Please enter Email address'})
}
else if(reg.test(userEmail) === false)
{
//alert("Email is Not Correct");
this.setState({email:'Email is Not Correct'})
return false;
}
else if(userPassword==""){
this.setState({email:'Please enter password'})
}
else{
fetch('http://siteURL/wetest/userlogin.php',{
method:'post',
header:{
'Accept': 'application/json',
'Content-type': 'application/json'
},
body:JSON.stringify({
email: userEmail,
password: userPassword
})
})
.then((response) => response.json())
.then((responseJson)=>{
let data = responseJson.data;
if (this.saveToStorage(data)){
this.setState({
validating: false
});
alert("Successfully Login");
/* Redirect to home page */
Actions.home()
} else {
alert("Wrong Login Details");
}
})
.catch((error)=>{
console.error(error);
});
}
Keyboard.dismiss();
}
render(){
return(
<View style={styles.container}>
<TextInput style={styles.inputBox}
underlineColorAndroid='rgba(0,0,0,0)'
placeholder="Email"
placeholderTextColor = "#ffffff"
selectionColor="#fff"
keyboardType="email-address"
onChangeText={userEmail => this.setState({userEmail})}
/>
<TextInput style={styles.inputBox}
underlineColorAndroid='rgba(0,0,0,0)'
placeholder="Password"
secureTextEntry={true}
placeholderTextColor = "#ffffff"
ref={(input) => this.password = input}
onChangeText={userPassword => this.setState({userPassword})}
/>
<TouchableOpacity style={styles.button} onPress={this.login} >
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</View>
)
}
async saveToStorage(userData){
if (userData) {
await AsyncStorage.setItem('user', JSON.stringify({
isLoggedIn: true,
authToken: userData.auth_token,
id: userData.user_id,
name: userData.user_login
})
);
return true;
}
return false;
}
}
我已经完成了这样的服务器站点代码。
*
<?php
include 'wp-load.php';
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$email = $obj['email'];
$password = $obj['password'];
$response = array(
'data' => array(),
'msg' => 'Invalid email or password',
'status' => false
);
if($obj['email']!=""){
$creds = array();
$creds['user_login'] = $email;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ){
echo json_encode('Wrong Details');
}
else{
$user = get_user_by( 'email', $email );
if ( $user ){
$password_check = wp_check_password( $_POST['password'], $user->user_pass, $user->ID );
if ( $password_check ){
/* Generate a unique auth token */
$token = wp_generate_password( 30 );
/* Store / Update auth token in the database */
if( update_user_meta( $user->ID, 'auth_token', $token ) ){
/* Return generated token and user ID*/
$response['status'] = true;
$response['data'] = array(
'auth_token' => $token,
'user_id' => $user->ID,
'user_login' => $user->user_login
);
$response['msg'] = 'Successfully Authenticated';
//echo json_encode($response);
}
}
}
}
}
else{
echo json_encode('try again');
}
?>
*
如果您能看到这两个代码,则将其写入登录并通过“ async saveToStorage”将数据保存在设备中。但是现在它给了我错误。 错误是“ json解析错误意外eof”。您可以在php代码中,我尝试通过json_encode返回数据。那也没有用。我可以得到一些。我想知道确切的错误在哪里?预先感谢。
答案 0 :(得分:0)
通常是json-parse-error-unexpected-e
尝试将对JSON的不可覆盖响应(例如html响应)转换为json时出现错误
为了安全起见,我通常先使用.then(res => res.text())
将其转换为字符串,然后使用console.log()或alert()来证明响应可转换为json或不转换,而不显示react-native的红框消息,
如果响应是有效的json,则可以使用JSON.parse(responseText)