Fetch is not working
See original GitHub issueEnvironment
Environment: OS: Windows 10 Node: 8.10.0 Yarn: 1.5.1 npm: 5.6.0 Watchman: Not Found Xcode: N/A Android Studio: Version 3.0.0.0 AI-171.4443003
Packages: (wanted => installed) react: 16.3.0-alpha.1 => 16.3.0-alpha.1 react-native: 0.54.2 => 0.54.2
Expected Behavior
I am waiting for successful JSON from server: {“…”}
Actual Behavior
I get
SyntaxError: Unexpected token b in JSON at position 0
. b is the first letter of word “badlogin”. It responds server when sent wrong combination of userName and password. But when I use Postman with the same key vaules combination on the same addres I get correct rosponse from the server.
Steps to Reproduce
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View, TouchableOpacity
} from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.btn}
onPress={this.login}>
<Text>Log in</Text>
</TouchableOpacity>
</View>
);
}
login = () => {
fetch('http://.....', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
userName: "react",
password: "123",
})
}).then((response) => response.json())
.then((responseJson) => {
console.log(responseJson.message);
if(responseJson.success === true) {
alert('successufuly loged');
}
else{
console.log(responseJson.message);
alert(responseJson.message);
}
})
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#2896d3',
paddingLeft: 40,
paddingRight: 40,
}, btn: {
alignSelf: 'stretch',
padding: 20,
alignItems: 'center',
backgroundColor: '#01c853',
}
});
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Javascript Fetch not getting a response - Stack Overflow
Javascript Fetch not getting a response · fetch is poorly supported, did you check that your browser supports it? developer.mozilla.org/en-US/docs/Web/API/ ...
Read more >Is the Fetch Rewards App Crashing? Here's How to Fix It!
1. Clear the Cache for Fetch Rewards · 2. Check Your Phone's Available Storage · 3. Uninstall and Reinstall the Fetch App.
Read more >How to Use the Fetch API (Correctly) - CODE Magazine
Navigate into the folder Samples-WebAPI and load that folder in Visual Studio Code or Visual Studio 2019. Open the appsettings.json file and ...
Read more >Serving - web.dev
The fetch event lets us intercept every network request made by the PWA in the service worker's scope, for both same-origin and cross-origin...
Read more >Fetch API - MDN Web Docs
The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I solved this issue by using FormData instead to prepare data for sending:
try doing this alert(JSON.stringify(responseJson));
Also, are you using the same user name and password because it gives badlogin in reponse on postman as well.