React native http fetch request not working

react native http request
react native network request failed
react-native fetch not working on android
react native fetch post not working
react-native fetch ssl android
react-native https not working
react-native allow http request android
fetch api not working in react-native

I'm new to react native trying to do the http request using fetch method (also tried axios). I'm using PHP as backend. What I'm trying is to print the JSON object returned from server in react native.

I'm sure that the response is returning from server cause I printed the result and it is a JSON array. But when I consoled the responseData using Chrome dev tools I got this []. I don't know where the mistake is.

index.js

    componentDidMount(){

        fetch('http://xxxxxxxxxx/result.php', {
            method: 'GET',
        }).then(response =>response.json())
          .then((responseData) =>
          {
              console.log(responseData);
              this.setState({
                  user: (responseData.name),
                  loaded:true,
              })

        });         
}
    render(){
        return(
        <Card>  
  <CardTitle style={styles.containerStyle} title={this.state.user} />
</Card>
        );
    }
}

Also my php is script is as follows

result.php

<?php
include 'db.php';
$check_json = file_get_contents('php://input');

$obj1= json_decode($check_json);
$uid =$obj1->{'uuid'};
$blood = $obj1->{'bloodgroup'};
$loc = $obj1->{'place'};
$prep =$mysqli->prepare("select token,name,uuid from signup where location 
like '".$loc."%' and bloodgroup= ?");
$prep->bind_param("s",$blood);
$prep->execute();
$result= $prep->get_result();
$payload= array();
while($row= $result->fetch_array(MYSQL_ASSOC)){
   $payload[]= array('name' =>$row['name']
                    'uuid' =>$row['name']);
}
$obj2 =json_encode($payload);
echo $obj2;
?>

and the response from server is

 [{"name":"angel","uuid":"b4b8a266-60cf-4e30-a778-596316d1a4a2"}]

Try using axios. You can check the docs here After you have done npm install axios and imported axios into your component you can try:

componentDidMount(){
axios
  .get('https://www.your-url.com/path-to-your-php-api')
  .then(({ data })=> {
    this.setState({ 
      user: data.name
    });
  })
  .catch((err)=> {})
}

if you still have problems, post the url that returns JSON so I can take a look.

Check your PHP API, if the object is returning empty, make sure to encode json. Here is an example you can try

$myObj->name = "Michel Arteta";
$myObj->age = 30;
$myObj->city = "New York";

$myJSON = json_encode($myObj);

echo $myJSON;

Fetch not working on Android 9 API 28 � Issue #24627 � facebook , same issue here! for testing purposes, we are using http for fetch data to our test physical mobile devices, if react-native block this capability, in� I'm new to react native trying to do the http request using fetch method (also tried axios). I'm using PHP as backend. What I'm trying is to print the JSON object returned from server in react native. I'm sure that the response is returning from server cause I printed the result and it is a JSON array.

Is your url is http ? Do you try on IOS ?

(if yes, you need add your domain to white list in XCode )

By default, iOS will block any request that's not encrypted using SSL. If you need to fetch from a cleartext URL (one that begins with http) you will first need to add an App Transport Security exception. If you know ahead of time what domains you will need access to, it is more secure to add exceptions just for those domains;

react-native fetch() method not working for HTTPS requests but is , When using the fetch() method with HTTPS I am getting a TypeError: Network request failed. I've tested this with different HTTP and HTTPS� React Native fetch() not working android. Ask Question Asked 2 years, 5 months ago. thanks to @mohsenomidi, this errors happens due to http request.

I've solved this.Problem was with the react part.I was trying to fetch this response returned in a different page.Actually the response returned from server was correct.

React native http fetch request not working, Try using axios. You can check the docs here After you have done npm install axios and imported axios into your component you can try: I am facing the same issue in android release apk. React native version is 61.5. Tried with android:usesCleartextTraffic="true"( tried with fetch and axios ,still not working). Also tried with http and https also, still no luck. Everything works fine in debug mode. 👍

Networking � React Native, Using Fetch. React Native provides the Fetch API for your networking needs. Fetch also takes an optional second argument that allows you to customize the HTTP request. You may The following options are currently not working with fetch. This post will give you an idea about how to Make HTTP Request to Fetch the Data From Web APIs in React Native. Here is an interesting example of posting and fetching the data from the server . Whenever you connect your application from the backend server (to get or post the data) you have to make an HTTP request.

React Native Fetch, Many mobile apps need to load resources from a remote URL. You may want to React Native provides the Fetch API for your networking needs. Fetch will seem The following options are currently not working with fetch. redirect: manual� react-native-background-fetch: 2.6.0 react-native-cli: 2.0.1 react-native-gesture-handler: 1.3.0. @amohit1315 your issue is not related to this case you are using HTTPS and have an issue, please open another case to report your problem, the workaround solution for http fetch only has been provided in my last comment.

Consuming REST APIs In React With Fetch And Axios — Smashing , Example of calling Web Service/API using React Native Fetch. //If response is not in json then in error .catch((error) => { //Error console.error(error); }); to trigger the request but If You need any data which needs to render while loading your� Always throws “network error” while do formdata post in react-native 1 React Native Fetch Remote Http request throws Network Request Failed (On Android Device) React-Native Network request failed on Android Emulator API 16-19 , Hi Everyone, I found this problem on my React-Native Android App. When I using a fetch to communicate with a

Comments
  • Thank you for this answer will try this.
  • Still same error getting []. json response is [{"name":"angel","uuid":"b4b8a266-60cf-4e30-a778-596316d1a4a2"}].I'm writing the response to a text file.I can't see this response by taking the url which i mentioned inside fetch.
  • What's your url that prints the json. Do you have it on a server?
  • Yes ,so i've printed the json object echo $obj2 but [] this is coming.But when i printed hii.it is coming .So problem is with server side i think is it??
  • yes, check your PHP api, I have added a code to test. Maybe this is the problem.