首页 文章

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

提问于
浏览
-3
_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和响应数据给我下面的错误:未处理的promise promise:SyntaxError:JSON解析错误:无法识别的令牌'<'

任何人都知道如何解决这个问题?

此外,当页面加载时,有时代码正常工作,只有在某些时候我收到此错误 .

谢谢 .

1 回答

  • 1

    @ QuentinPointy

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

    最后我自己解决了我的问题 . 谢谢你们两个人的时间和建议 .

相关问题