2017-06-22 72 views
0

我剛剛從反應原生API獲取數據。代碼就像這樣。反應原生json響應無法解析

fetch(Config.SERVER_URL + '/api/login/tokencheck/' + accessToken, { 
    method: "GET", 
    headers: { 
     'Accept': 'application/json', 
     'Content-Type': 'application/json', 
    } 
}) 
.then((response) => response.text()) 
.then((responseJson) => { 

    alert(responseJson); 

    alert(JSON.stringify(responseJson)); 


}) 
.catch((error) => { 
    alert("error");   
    alert(error); 
    console.log(error); 
}) 

的問題是,在在回調的第一個警報,它告訴我,有一個像這樣{"name": "Abigail","state": "CA"}

JSON格式的字符串,但是,當我轉換是respon使用JSON.parse JSON它返回錯誤意外的標記「 ? 。但是當我主動反應原生Remote Debuger時,它工作得很好。任何解決方案

回答

0

即使我之前遇到過這個問題。我認爲這應該做到這一點。

componentDidMount() { 
      return fetch('http://example.com/getData') 
       .then((response) => response.json()) 
       .then((responseJson) => { 
        this.setState({ 
         isLoading: false, 
         data: responseJson, 
        }, function() { 
         // do something with new state 
        }); 
       }) 
       .catch((error) => { 
        console.error(error); 
       }); 
     } 

我希望幫助:)

+0

您可以使用您的數據是這樣的。 ** ** this.state.data –