React-native: json parse error unrecognized token '<' react native

Created on 9 Mar 2018  Â·  4Comments  Â·  Source: facebook/react-native

hey heros
i m really in a big trouble with fetching data from API rest asp.net using my localhost i change localhost to ip adress but not work
`
import React, { Component } from 'react';
import {
TextInput,
StyleSheet,
Text,
View,
TouchableOpacity,
KeyboardAvoidingView,
AsyncStorage,
Platform

} from 'react-native';

import {StckNavigator} from 'react-navigation';

export default class Login extends Component {

constructor(props) {
super(props);

this.state = {
    username:'',
    password:'',
};

}

componentDidMount(){

this._loadInitialState().done();
}

_loadInitialState = async () => {

var value = await AsyncStorage.getItem('users');
if(value!==null){
this.props.navigation.navigate('Profile');
}
}

render() {
return (

   <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})}
         underlineColorAndroid = 'transparent'
      />
      <TouchableOpacity
     style={styles.btn}
     onPress ={this.login}
      >
        <Text> Log in </Text>

      </TouchableOpacity>

   </View>


);

}

login = ()=>{

fetch('http://192.168.1.26:1543/api/Users',{

    method:'POST',
    header : {
  //   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('users',res.users);
    this.props.navigation.navigate('Profile');
  }
  else {
    alert('test test not work',res.message);
  }


})

 .done();

}
}

const styles = StyleSheet.create({

wrapper :{

flex : 1 ,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#2896d3',
paddingLeft : 40,
paddingRight: 40,
},
header: {
fontSize: 24,
marginBottom: 60,
color : '#fff',
fontWeight : 'bold',
},
textInput: {

alignSelf: 'stretch',
padding: 16,
marginBottom : 20,
backgroundColor:'#fff',
},
btn: {

alignSelf: 'stretch',
padding: 16,
marginBottom : 20,
alignItems : 'center',
backgroundColor:'#01c853',
}

});

`

i do every things without any results but when i comment ''' .then((response)=> response.json()) ''' that s work but without any data
please i need your help ..

AsyncStorage Ran Commands For Stack Overflow Locked

Most helpful comment

This happen when you receive response from the server with HTML format (error) instead of JSON format correct response.

All 4 comments

Thanks for posting this! It looks like your issue may be incomplete. Are all the fields required by the Issue Template filled out?

You may safely ignore this if you believe your issue contains all the relevant information. Thank you for your contributions.

How to Contribute • What to Expect from Maintainers

This happen when you receive response from the server with HTML format (error) instead of JSON format correct response.

This issue looks like a question that would be best asked on StackOverflow.

StackOverflow is amazing for Q&A: it has a reputation system, voting, the ability to mark a question as answered. Because of the reputation system it is likely the community will see and answer your question there. This also helps us use the GitHub bug tracker for bugs only.

Will close this as this is really a question that should be asked on StackOverflow.

This happen when you receive response from the server with HTML format (error) instead of JSON format correct response.

then how to solve it same code working in IOS correctly but on android it is not

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TrakBit picture TrakBit  Â·  3Comments

lazywei picture lazywei  Â·  3Comments

josev55 picture josev55  Â·  3Comments

jlongster picture jlongster  Â·  3Comments

WG-Com picture WG-Com  Â·  3Comments