React Native API json解析数据错误:未处理的promise promise:SyntaxError:JSON解析错误:无法识别的令牌'<'

时间:2018-06-15 13:35:06

标签: javascript react-native

    _getNotificationDetails() {
    this._showLoading();
    AsyncStorage.getItem('auth_token', (errs, result) => {
      if (!errs) {
        if (result !== null) {
          auth_token = result;
          this.setState({ auth_token: result });
        }
      }
    }).then(() => {
      const notificationsData = [];
      let authToken = this.state.auth_token;
      apitoken = `Bearer ${authToken}`;
      fetch(AppConstants.getNotificationDetailsAPIURL, {
        method: 'get',
        headers: new Headers({
          'Authorization': apitoken,
          'cache-control': 'no-cache',
          'Accept': "application/json",
        })
      }).then((response) => {
        this.setState({ loading: false });
        this._hideLoading();
        return response.json();
      }).then((responseData) => {
        this.setState({ loading: false });
        this._hideLoading();
        if (responseData.data !== null) {
          this.setState({ loading: false });
          this._hideLoading();
          this.setState({ notificationsData: responseData.data });
          var serialNumber = 0;
          const sortable = responseData.data;
          console.log(sortable);
          sortable.map(function (item, index) {
            var getNotificationDuration = this._getNotificationDuration(sortable[index].created_at);
            notificationsData.push(
              <TouchableOpacity key={`${serialNumber}`} onPress={() => ""}>
                <View style={styles.dataViewNotification} key={`${serialNumber}`}>
                  <View style={styles.fixedBoxViewDesignFirstNotification}>
                    <View style={styles.friendsViewRidesDesignNotification}>
                      <View style={styles.friendsViewInnerBoxDesignNotification}>
                        <View styles={{ width: 80, paddingTop: 20, borderColor: 'black', borderRadius: 1 }}>
                          <View style={styles.middleBoxScrollViewImageDesign}>
                            <Image
                              source={
                                __DEV__
                                  ? require('../assets/images/profileIcons/Profile-Bike.png')
                                  : require('../assets/images/profileIcons/Profile-Bike.png')
                              }
                              style={styles.notificationProfileImageDesign}
                            />
                          </View>
                        </View>
                        <View style={styles.boxMiddleContentViewDesignFriendsMainViewNotification}>
                          {this.state.fontLoaded ? <Text style={styles.friendsCountDesignFriendsTextNotification}>{sortable[index].body ? sortable[index].body : ''}</Text> : null}
                          {this.state.fontLoaded ? <Text style={styles.friendsCountDesignFriendsTextTypeNotification}>{getNotificationDuration}</Text> : null}
                        </View>
                        <View style={styles.boxLastContentViewDesignFriendsMainViewNotification}>
                          <TouchableOpacity onPress={() => { this.setState({ checked: !this.state.checked }) }} style={styles.checkBoxAgreeTermsAndConditionOuterDesign}>
                            <Entypo size={25} name="chevron-small-right" color="#292929" />
                          </TouchableOpacity>
                        </View>
                      </View>
                    </View>
                  </View>
                </View>
              </TouchableOpacity >
            );
          }.bind(this));

          console.log(notificationsData);
          this.setState({ notificationDataView: notificationsData });
        }
      });
    });
  }

以下是我的api共鸣:

{
    "total": 2,
    "offset": 0,
    "limit": 100,
    "data": [
        {
            "id": 19,
            "body": "Wilson has sent you a friend request.",
            "associated_object_id": 2,
            "associated_object_type": "User",
            "read": false,
            "created_at": "2018-06-13T14:49:40.900+00:00"
        },
        {
            "id": 18,
            "body": "David accepted your friend request!",
            "associated_object_id": 2,
            "associated_object_type": "User",
            "read": false,
            "created_at": "2018-06-13T14:49:37.956+00:00"
        }
    ]
}

我使用了fetch API,响应数据给了我以下错误: 未处理的承诺拒绝:SyntaxError:JSON解析错误:无法识别的令牌'&lt;'

有人知道如何解决这个问题吗?

此外,当页面加载时,有时代码正常运行,但有时我才会收到此错误。

谢谢。

1 个答案:

答案 0 :(得分:1)

@ QuentinPointy

我也是软件工程师。 我已经检查了http://json.parser.online.fr/中的响应。 它给了我适当的答复。

最后,我自己解决了我的问题。 谢谢你们的宝贵时间和建议。