2017-08-15 47 views
-1
getData() { 
return fetch("http://bitcfeedcms.rf.gd/script.php") 
    .then(response => { 
    console.log(response); 
    response.json(); 
    }) 
    .then(responseJson => { 
    this.setState({ data: responseJson }); 
    }) 
    .catch(error => { 
    console.error(error); 
    }); 

}試圖在反應本地,但沒有得到正確響應於經由URL加載JSON

我還通過將ΔL= 1像「bitcfeedcms.rf.gd/script.php?l=1試圖」。 主要的json文件是「bitcfeedcms.rf.gd/feed_data.json」。所以,我想「http://bitcfeedcms.rf.gd/feed_data.json?l=1」這太,但沒有改變

什麼我現在做的就是JSON數據,並在我的應用程序使用它...請幫助

+0

你看到的console.log? –

+0

是的...我檢查。 –

+0

響應日誌在這裏... http://bitcfeedcms.rf.gd/1.png –

回答

0

http://bitcfeedcms.rf.gd/script.php,這個鏈接將返回多套JSON的。 (FeedTitle「:」Debotos「,」FeedDescription「:」First Feed Testing .....「),{」FeedTitle「:」Akash「,」FeedDescription「:」創建一個名爲\ 「Khulna Sparkers \」「},{」FeedTitle「:」Ripon「,」FeedDescription「:」我的兄弟和我最親密的個人之一「}]

試試這個。 。 。

getData() { 
fetch("http://bitcfeedcms.rf.gd/script.php") 
    .then(response => { 
    console.log(response); 
    response.json(); 
    }) 
    .then(responseJson => { 
     var length = responseJson.length; 
     for (var a = 0; a<length;a++) { 
      var FeedTitle = responseJson[a].FeedTitle; //<-variable from your response json 
      var FeedDescription = responseJson[a].FeedDescription; //<-from your response json 
      console.log(FeedTitle); 
      console.log(FeedDescription); 
     } 
    }) 
    .catch(error => { 
    console.error(error); 
    }); 
} 
+0

響應日誌在這裏http://bitcfeedcms.rf.gd/1.png 其他網址(for script.php)的迴應是一樣的 –

+0

請檢查你的引號 –

+0

看看這一行{「FeedTitle」:「Akash」,「FeedDescription」:「創建一個名爲」Khulna Sparkers \ 「」}我想你有一個錯誤的Json格式。 json的數據必須是這樣的{「var」:「value」} –

1

您正在使用箭頭功能錯誤。取而代之的是:

fetch("http://bitcfeedcms.rf.gd/script.php") 
    .then(response => { 
    console.log(response); 
    response.json(); 
    }) 
    .then(responseJson => { 
    this.setState({ data: responseJson }); 
    }) 

您應該返回response.json()

fetch("http://bitcfeedcms.rf.gd/script.php") 
    .then(response => { 
    console.log(response); 
    return response.json(); 
    }) 
    .then(responseJson => { 
    this.setState({ data: responseJson }); 
    }) 

這樣你可以在未來then達到responseJson。此外,如果您的應用抱怨fetch() network request failed可能是Info.plist或Manifest配置錯誤。看到這個topic

在iOS設備上,你可以嘗試用此https虛擬JSON的URL相同的請求: https://jsonplaceholder.typicode.com/posts/1

+0

http://bitcfeedcms.rf.gd/1.png響應數據。 它沒有給出正確的數據.... –

+0

你試過我在這裏寫的嗎? – eden

+0

是的,我試着用你的代碼,然後(屏幕截圖)結果出現。 –