javascript - Trying to load JSON via url in react-native but not getting correct response - Stack Overflow

getData() {return fetch(".php").then(response => {console.log(response);response.json();}

getData() {
return fetch(".php")
  .then(response => {
    console.log(response);
    response.json();
  })
  .then(responseJson => {
    this.setState({ data: responseJson });
  })
  .catch(error => {
    console.error(error);
  });

}

I also tried by putting ?l=1 like " bitcfeedcms.rf.gd/script.php?l=1 ". The main json file is " bitcfeedcms.rf.gd/feed_data.json ". So i tried ".json?l=1" this too but nothing changed

What i have to do now to get the json data and use it in my app...Please help

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);
  });

}

I also tried by putting ?l=1 like " bitcfeedcms.rf.gd/script.php?l=1 ". The main json file is " bitcfeedcms.rf.gd/feed_data.json ". So i tried "http://bitcfeedcms.rf.gd/feed_data.json?l=1" this too but nothing changed

What i have to do now to get the json data and use it in my app...Please help

Share Improve this question edited Aug 22, 2017 at 16:08 Debotos Das asked Aug 15, 2017 at 8:07 Debotos DasDebotos Das 5691 gold badge6 silver badges19 bronze badges 5
  • do you see the console.log ? – Rizal Sidik Commented Aug 15, 2017 at 8:23
  • yes ...i checked. – Debotos Das Commented Aug 15, 2017 at 16:58
  • Response log is here...bitcfeedcms.rf.gd/1.png – Debotos Das Commented Aug 15, 2017 at 17:06
  • 1 I think your issue lies with the php script. Did you try making the request with Postman? The response es back as HTML. – MattyK14 Commented Aug 15, 2017 at 17:22
  • the postman giving me html as response....see here the response of postman bitcfeedcms.rf.gd/postman%20error.png – Debotos Das Commented Aug 22, 2017 at 16:07
Add a ment  | 

3 Answers 3

Reset to default 4

You are using the arrow function wrong. Instead of this:

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

You should return the response.json()

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

This way you can reach responseJson in the next then.

Also, if your app plains about fetch() network request failed probably it's about Info.plist or Manifest configuration error. See this topic.

For iOS, you can try the same request with this https dummy json url: https://jsonplaceholder.typicode./posts/1

http://bitcfeedcms.rf.gd/script.php , this link returns multiple sets of JSON.

([{"FeedTitle":"Debotos","FeedDescription":"First Feed Testing....."},{"FeedTitle":"Akash","FeedDescription":"Creating a clan named \"Khulna Sparkers\""},{"FeedTitle":"Ripon","FeedDescription":"My brother and my one of the closest individual"}])

try this . . .

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);
  });
}

try axios

npm install --save axios

state = {data:[]};
    ponentWillMount() {
                 axios.get('http://bitcfeedcms.rf.gd/script.php')
                 .then(response =>this.setState({data:response.data}));
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745064234a4609157.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信