question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetch is not working

See original GitHub issue

Environment

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:closed
  • Created 6 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
vladinko0commented, Mar 15, 2018

I solved this issue by using FormData instead to prepare data for sending:

......
login = () => {
    var formData = new FormData();
    formData.append('username', 'react');
    formData.append('password', '123');

    fetch('http://......', {
     method: 'POST',
     body: formData
........

1reaction
sfm2222commented, Mar 14, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found