无法在警告框中显示值

时间:2017-02-28 08:07:05

标签: json react-native alert fetch

 import React, { Component } from 'react';
    import {
      AppRegistry,
      StyleSheet,
      Text,
      View,
      Navigator,
      TouchableOpacity,
      Image,
      TextInput,
      TouchableHighlight,
      Alert
    } from 'react-native';
    import Button from 'react-native-button'
    import {Actions} from 'react-native-router-flux'
    import Home from './Home'

    export class Bp extends Component{
        _onPressButtonPOST(){
            fetch("URL_HERE", {
                method: "POST",
                 headers: {
                     'Accept': 'application/json',
                     'Content-Type': 'application/json',
                 },
                body: JSON.stringify({'entryDate':"2/27/2017 11:11 AM",'syst':"90"})})
            .then((response) => response.json())
            .then((responseData) => {
                Alert.alert(
                    " data",
                    " data - " + JSON.stringify(responseData.body)
                )
               })
            .done();
        }

     render(){
            return(
                <View>
             <TouchableHighlight onPress={this._onPressButtonPOST} >
                        <Text>Add</Text>
                    </TouchableHighlight>
                   </View>
                );
        }
    }

    module.exports = Bp;

在上面的代码中,我无法在警告框中显示值。我收到错误,如'undefined',我想存储值并在警告框或现场显示相同,请帮助我,我如何编辑代码?感谢

1 个答案:

答案 0 :(得分:1)

你改写如下:

  import React, { Component } from 'react';
            import {
              AppRegistry,
              StyleSheet,
              Text,
              View,
              Navigator,
              TouchableOpacity,
              Image,
              TextInput,
              TouchableHighlight,
              Alert
            } from 'react-native';
            import Button from 'react-native-button'
            import {Actions} from 'react-native-router-flux'
            import Home from './Home'

            export class Bp extends Component{

            state = {
                     responseBody: ""
            };

            _onPressButtonPOST() {
              let customBody = {entryDate:"2/27/2017 11:11 AM",syst:"90"};
              return fetch("URL", {
                                   method: "POST",
                                   headers: {
                                             'Accept': 'application/json',
                                              'Content-Type': 'application/json',
                                   },
                                    body: JSON.stringify(customBody)
               }).then((responseData) => {
                                  this.setState({
                                  responseBody: JSON.stringify(customBody)
                                  });
               }).done()
             }

            render() {
             return (
                     <View>
                          <TouchableOpacity onPress={this._onPressButtonPOST.bind(this)}
                              style={{backgroundColor:"red",width:100,height:29}}>
                              {this._renderBody(this.state.responseBody) }
                        </TouchableOpacity>
                    </View >);

             }

            _renderBody(responseBody) {
                 if (responseBody) {
                 var parsedBody = JSON.parse(responseBody);
                 return (<View>
                         <Text>{parsedBody.entryDate}</Text>
                        <Text>{parsedBody.syst}</Text>
                </View>);
                 }
             }
          }

           module.exports = Bp;