【问题标题】:fetch is not work in IOS simulator with React Native 0.44fetch 在带有 React Native 0.44 的 IOS 模拟器中不起作用
【发布时间】:2018-03-18 03:35:50
【问题描述】:

Fetch 在带有 React Native 0.44 的 IOS 模拟器中不起作用。 我正在尝试使用 fetch 在本机反应中创建登录表单。

这是我的代码,

login.ios.js

import React, { Component } from 'react';
import { StyleSheet,TextInput,ActivityIndicatorIOS,TouchableHighlight, Text, View } from 'react-native';

class Login extends Component {
  constructor(){
    super();
    this.state = {
      reg_mob_no: "",
      password: "",
      error: "",
      showProgress: false,
    }
  }


  async onLoginPressed() {
    this.setState({showProgress: true})
    try {
      let response = await fetch('http://example.in/api/premiumuser/login', {
                              method: 'POST',
                              headers: {
                                'Accept': 'application/json',
                                'Content-Type': 'application/json',
                              },
                              body: JSON.stringify({
                                  reg_mob_no: this.state.reg_mob_no,
                                  password: this.state.password,
                              })
                            });
      let res = await response.text();
      console.log("res is:" + res );
    } catch(error) {
    }
  }
  render() {

    return (
      <View style={styles.container}>
        <Text style={styles.heading}>
              Login Form!
            </Text>
        <TextInput
          onChangeText={ (text)=> this.setState({reg_mob_no: text}) }
          style={styles.input} placeholder="Registered Mobile No">
        </TextInput>
        <TextInput
          onChangeText={ (text)=> this.setState({password: text}) }
          style={styles.input}
          placeholder="Password"
          secureTextEntry={true}>
        </TextInput>
        <TouchableHighlight onPress={this.onLoginPressed.bind(this)} style={styles.button}>
          <Text style={styles.buttonText}>
            Login
          </Text>
        </TouchableHighlight>

        <Text style={styles.error}>
          {this.state.error}
        </Text>


      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'flex-start',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    padding: 10,
    paddingTop: 80
  },
  input: {
    height: 50,
    marginTop: 10,
    padding: 4,
    fontSize: 18,
    borderWidth: 1,
    borderColor: '#48bbec'
  },
  button: {
    height: 50,
    backgroundColor: '#48BBEC',
    alignSelf: 'stretch',
    marginTop: 10,
    justifyContent: 'center'
  },
  buttonText: {
    fontSize: 22,
    color: '#FFF',
    alignSelf: 'center'
  },
  heading: {
    fontSize: 30,
  },
  error: {
    color: 'red',
    paddingTop: 10
  },
  success: {
    color: 'green',
    paddingTop: 10
  },
  loader: {
    marginTop: 20
  }
});


module.exports = Login;

点击注册按钮时,我没有收到来自服务器的任何响应。

与 expo 一起使用但在 ios 模拟器中无法使用的相同来源。开发人员工具什么也没显示。

如何解决这个问题.. 请帮忙!!!

React Native 版本:0.44 平台:IOS 开发操作系统:mac os 10.12 构建工具:Xcode 9

【问题讨论】:

标签: javascript api react-native ios-simulator fetch-api


【解决方案1】:

尝试在获取选项中包含键值对credentials: 'include'。 RN 0.44 引入了带有凭据的 XHR 标志,如您所见 here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2019-04-15
    • 2016-08-12
    • 2019-08-07
    • 2018-10-29
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多