Json response reactnative 获取登录数据

Json response reactnative fetch login data

我正在尝试使用下面的登录获取方法获取我的登录数据,但出现错误。 我不确定它为什么会引发此错误以及哪里出错了。

我收到这个错误

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'res._bodyText')]

登录获取方式

createUser: (username, password) => {
    return (
      fetch('http://192.168.0.140:80/auth/login/', {
        method: 'POST',
        dataType: 'jsonp',
        credentials: 'omit',
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
        },
        body: JSON.stringify({
          username: username,
          password: password
        })
      })


    )
  },

在我的 login.js

state = {
    username: '',
    password: ''
  }

login = () => {
    if (this.state.username === '') {
      alert('Enter username !')
    } else if (this.state.password === '') {
      alert('Enter Password !')
    } else {
      api
        .createUser(this.state.username, this.state.password)
        .then((response) => response.json())
        //If response is in json then in success
        .then((responseJson) => {
          //Success
          console.log(responseJson)
        })
        //If response is not in json then in error
        .catch((error) => {
          //Error
          console.error(error)
          alert(error)
        })
    }
  }

调用登录()

<TouchableNativeFeedback onPress={() => this.login()}>
                    <View style={styles.buttonContainer}>
                      <Text bold style={styles.buttonText}>
                        Login
                      </Text>
                    </View>
                  </TouchableNativeFeedback>

这可以帮助你:

import {Alert} from 'react-native'

login = () => {
    if (this.state.username === '') {
      Alert.alert('Enter username !')
    } else if (this.state.password === '') {
      Alert.alert('Enter Password !')
    } else {
      api
        .createUser(this.state.username, this.state.password)
        .then((response) => {
            if(response.ok) {
              Alert.alert('Login successful')
            }else{
              Alert.alert('Check credentials')
            }
        })
        //If response is in json then in success
        
        //If response is not in json then in error
        .catch((error) => {
          //Error
          Alert.alert('Check credentials')
        })
    }
  }