在React本机中创建一个项目,对于登录页面,我从url中获取json数据。为此,我编写了这样的代码,
import React, {Component} from 'react';
import { StyleSheet,
Text,
View,
TextInput,
TouchableHighlight,
Alert,
TouchableOpacity,
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
export default class Loginscreen extends Component{
constructor() {
super();
this.state = {
username : '',
password : '',
}
}
navigateToHomescreen = () => {
this.props.navigation.navigate('Homescreen');
}
submit()
{
let collection={ }
collection.email=this.state.email,
collection.password=this.state.password
// console.warn(collection);
var url = 'my url';
fetch(url, {
method: 'POST', // or 'PUT'
body: JSON.stringify(collection), // data can be `string` or {object}!
headers: new Headers({
'Content-Type': 'application/json'
})
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:',response));// this._navigateToHomescreen
}
onClickListener = (viewId) => {
Alert.alert("Alert", "Button pressed "+viewId);
}
render() {
return (
<View style={styles.container}>
<View>
{/* <FontAwesome>{Icons.chevronLeft}</FontAwesome> */}
<Text style={styles.inputHeading}>Staff Login</Text>
</View>
<View style={styles.inputContainer}>
<Icon style={styles.inputIcon} name='user' size={30} ></Icon>
<TextInput style={styles.inputs}
placeholder="Username"
keyboardType="email-address"
underlineColorAndroid='transparent'
onChangeText={(email) => this.setState({email})}
/>
</View>
<View style={styles.inputContainer}>
<Icon style={styles.inputIcon} name='key' size={30} ></Icon>
<TextInput style={styles.inputs}
placeholder="Password"
secureTextEntry={true}
underlineColorAndroid='transparent'
onChangeText={(password) => this.setState({password})}
/>
</View>
<TouchableOpacity style={[styles.buttonContainer, styles.loginButton]} onPress={() => this.submit()}>
<Text style={styles.loginText}>Login</Text>
</TouchableOpacity>
<TouchableHighlight style={styles.buttonContainer} onPress={() => this.onClickListener('restore_password')}>
<Text>Forgot your password?</Text>
</TouchableHighlight>
</View>
);
}
}
在执行此操作时会收到类似'console.error:“ Error”:,{}的错误 请任何人帮助我,我陷入了这个错误。还告诉我如何从登录页面导航到主页。