首页 > 解决方案 > React Native 中的 Asp.net Webservice 响应获取

问题描述

我们正在尝试在我们的 react native 中获取 webservices json 响应。我们可以在以不同方式尝试后获取。我们来自网络服务的 Json 响应是这样的{

“表”:[{“代码”:“M251”,“名字”:“MINAL JAGDISH TRIVEDI”}]}

我们在本机反应中编写的代码以获取此响应是

import React from 'react';
import { FlatList, ActivityIndicator, Text, View  } from 'react-native';

export default class HomeScreen extends React.Component {

  constructor(props){
    super(props);
    this.state ={ isLoading: true}
  }

  componentDidMount(){
     return fetch('URL',{ 
      method: 'POST',
      headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
      },
   })
      .then((response) => response.json())
      .then((responseJson) => {

        this.setState({
          isLoading: false,
          dataSource: responseJson.Table,

        });

      })
      .catch((error) =>{
        console.error(error);
      });
  }



  render(){

    if(this.state.isLoading){
      return(
        <View style={{flex: 1, padding: 20}}>
          <ActivityIndicator/>
        </View>
      )
    }

    return(
      <View style={{flex: 1, paddingTop:20}}>
        <FlatList
          data={this.state.dataSource}

          renderItem={({item}) => <Text>{item.code}, {item.firstname}</Text>}

          keyExtractor={({id}, index) => id}
        />
      </View>
    );

  }
}

如果有人已经遇到此问题,请提供解决方案。

标签: asp.netreact-nativeweb-services

解决方案


推荐阅读