Witam, podczas próby logowania do aplikacji przez API wyskakuje błąd: SyntaxError: JSON Parse error: Unexpected EOF. Szukam już jakiś czas rozwiązania, ale nic sensownego nie znalazłem. Bardzo proszę o pomoc i z góry dziękuje.
import * as React from 'react';
import { View, Text, StyleSheet, TextInput, KeyboardAvoidingView, TouchableOpacity, AsyncStorage, Alert } from 'react-native';
export default class Login extends React.Component {
constructor(props){
super(props);
this.state = {
username: ' ',
password: ' '
}
}
render() {
return(
<KeyboardAvoidingView behavior='padding' style={styles.wrapper}>
<View style={styles.container}>
<Text style={styles.header}>- LOGIN -</Text>
<TextInput style={styles.textInput} placeholder='Username' onChangeText={ (username) => this.setState({username}) } underlineColorAndroid='transparent'/>
<TextInput style={styles.textInput} placeholder='Password' onChangeText={ (password) => this.setState({password}) } secureTextEntry={true} underlineColorAndroid='transparent'/>
<TouchableOpacity style={styles.btn} onPress={this.login}>
<Text>Log in</Text>
</TouchableOpacity>
</View>
</KeyboardAvoidingView>
);
}
login = () => {
fetch('http://endpoint', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password,
})
})
.then((response) => response.json())
.then((res) => {
if(res.success === true) {
AsyncStorage.setItem('user', res.user);
this.props.navigation.navigate('Profile');
}
else{
alert(res.message);
}
})
.done();
}
}