【问题标题】:pots API [object object ] in react native反应原生的pots API [object object]
【发布时间】:2020-04-14 15:53:33
【问题描述】:

我在向服务器发送数据时遇到问题。 当我尝试通过邮递员成功提交数据时,回复如下 如果我激活 '内容类型':'应用程序/json'

我有一个问题是 JSON 解析错误:无法识别的令牌 '

{
    "status": 200,
    "message": "success login",
    "id_kurir": "3",
    "username": "tester",
}

当我尝试使用代码时,我收到一个错误,即 [object object]

这是我的代码:

constructor(props) {
    super(props);
    this.state = {
      Uname : '',
      Upass : ''
    }
  }

  login= ()=>{
    const {Uname,Upass} = this.state;
    fetch('https://example.com/login', {
      method: 'POST',
      // headers: {
      //   'Accept' : 'application/json',
      //   'Content-Type': 'application/json'
      //},
              body: JSON.stringify({
              username: Uname,
              password: Upass
            })
      })
      .then((response) => response.json())
      .then((responseJson) => {
          alert(responseJson);
          console.log(JSON.stringify(responseJson));
        }).catch((error)=>{
          console.log(error);
        })
      Keyboard.dismiss();
  }



           <Form style={styles.mainForm}>
              <Item style={styles.formItems}>
                <Input placeholder="Username" style={styles.Input} onChangeText={Uname => this.setState({Uname})}/>
              </Item>
              <Item style={styles.formItems}>
              <Input style={styles.Input} secureTextEntry={true} onChangeText={(Upass) => this.setState({Upass})}/>
              </Item>

              <View style={styles.Button}>
                {/* <Button block style={styles.mainBtn} onPress={() => this.props.navigation.navigate('home')}> */}
                <Button block info style={styles.mainBtn} onPress={this.login}>
                  <Text style={styles.btnText}>Submit</Text>
                </Button>
              </View>
            </Form>

错在哪里?

【问题讨论】:

  • 试试 alert(JSON.stringify(responseJson));因为警报不能显示对象。
  • 完成了,我已经正确填写了用户名和密码,但是为什么没有找到响应?即使我已经尝试使用 Postman 正确填写用户名和密码并且结果是合适的......

标签: javascript reactjs api react-native


【解决方案1】:

服务器可能出现 404 或 500 错误。代替 response.json() 使用 response.text() 您将获得文本中的 html。

我假设您的服务器没有采用 JSON 格式的正文

尝试使用下面的代码。

选项 1:

fetch('https://example.com/login', {
    method: 'POST',
    body: JSON.stringify({
        username: Uname,
        password: Upass
    })
}).then(response => response.text()).then((text) => {
  if (Platform.OS === 'android') {
    text = text.replace(/\r?\n/g, '').replace(/[\u0080-\uFFFF]/g, ''); 
    // If android, remove unwanted chars. 
  }
  return text;
}).then(response => JSON.parse(response));

选项 2:

fetch('https://example.com/login', {
    method: 'POST',
    body: JSON.stringify({
        username: Uname,
        password: Upass
    })
}).then((response) => response.text()).then((responseJson) => {
    alert(JSON.stringify(responseJson));
    console.log(JSON.stringify(responseJson));
}).catch((error) => {
    alert(JSON.stringify(error));
    console.log(error);
})

【讨论】:

  • 我尝试使用第二个选项但是在控制台响应后 [object object]
  • 原来...你没有权限访问此服务器上的 /,
  • 这是您服务器端的问题,尝试修复它并使用正常的 fetch 调用重试,您将得到响应。
猜你喜欢
  • 1970-01-01
  • 2020-01-26
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2018-02-26
  • 2021-09-14
  • 2022-08-22
相关资源
最近更新 更多