JSON解析错误:无法识别的令牌'<'-在React Native中

时间:2019-03-10 12:22:29

标签: react-native

我在React Native中有问题。该消息就是这样的JSON Parse error: Unrecognized token '<'。当我抓取时会发生这种情况。

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

export default class FetchExample extends React.Component {

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

  componentDidMount() {
    return fetch("http://10.0.3.2:80/api/user", {
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json, text/plain, */*"
        },
        method: 'GET'
    }).then((response) => response.json())
      .then((responseJson) => {

        this.setState({
          isLoading: false,
          dataSource: responseJson.user,
        }, function(){

        });

      })
      .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.email}</Text>}
          keyExtractor={({id}, index) => id}
        />
      </View>
    );
  }
}

这是我的JSON。我在Laravel中建立。

return->response()->json(['user', $user])
["user",[{"id":1,"name":"","email":"danang@gmail.com","email_verified_at":null,"created_at":null,"updated_at":null}]]

1 个答案:

答案 0 :(得分:0)

我认为您的json格式不正确 请尝试使用此json

     {
     "user":[{
     "id":1,
     "name":"",
     "email":"danang@gmail.com",
     "email_verified_at":null,
     "created_at":null,
     "updated_at":null}]}