首页 文章

在React native中获取Undefined

提问于
浏览
0
import React, { Component } from 'react';
import { View, Text } from 'react-native';

class HttpExample extends Component {
   state = {
      data: ''
   }
   componentDidMount = () => {
    fetch("https://jsonplaceholder.typicode.com/posts/1", { --this is fake url 
        method: 'POST',
        headers: new Headers({
                   'Content-Type': 'application/x-www-form-urlencoded', // <-- Specifying the Content-Type
          }),
        body: "param1=value1&param2=value2" // <-- Post parameters
      })
      .then((response) => 
          response.text()
      )
      .then((responseText) => {
        alert(responseText.id);
        this.setState({
            data: responseText
         })
      })
      .catch((error) => {
          console.error(error);
      });
   }
   render() {
      return (
         <View>
            <Text>
               {this.state.data.id}
            </Text>
         </View>
      )
   }
}
export default HttpExample;

如果我在警报中使用警报(ResponseText)我正在获取o / p但是当我试图从我的对象中获得单独的值时它会返回undefined

o / p:“id”:“1”,“computeId”:“USR00001”处于警戒状态

1 回答

  • 0
    .then((response) => response.text())
    

    .then((response) => response.json())
    

    我想你需要json风格的回应 .

相关问题