Falló la Solicitud de Red fetch() de React Native


Cuando creo un proyecto nuevo usando react-native init (RN versión 0.29.1) y pongo una búsqueda en el método render a la API pública de facebook demo movie, arroja un Network Request Failed. Hay un seguimiento de pila muy inútil y no puedo depurar las solicitudes de red en la consola de Chrome. Aquí está la búsqueda que estoy enviando:

fetch('http://facebook.github.io/react-native/movies.json')
      .then((response) => response.json())
      .then((responseJson) => {
        return responseJson.movies;
      })
      .catch((error) => {
        console.error(error);
      });
Author: David Nathan, 2016-07-17

10 answers

El problema aquí es que iOS no permite solicitudes HTTP por defecto, solo HTTPS. Si desea habilitar las solicitudes HTTP agregue esto a su info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
 110
Author: Alek Hurst,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-04 20:05:27

No se recomienda permitir todos los dominios para http. Haga una excepción solo para los dominios necesarios.

Fuente: Configuración de Excepciones de Seguridad de Transporte de Aplicaciones en iOS 9 y OSX 10.11

Agregue lo siguiente a la información.archivo plist de su aplicación:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>
 50
Author: Lavi Avigdor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-24 18:46:09

React Native Docs da la respuesta a esto.

Apple ha bloqueado la carga implícita de recursos HTTP cleartext. Así que tenemos que añadir la siguiente información de nuestro proyecto.archivo plist (o equivalente).

<key>NSExceptionDomains</key>
<dict>
    <key>localhost</key>
    <dict>
        <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
        <true/>
    </dict>
</dict>

React Native Docs - > Integración Con Aplicaciones Existentes - > App Transport Security

 6
Author: Ocean Liu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-29 04:50:26

El problema puede estar en la configuración del servidor.

Android 7.0 tiene un error descrito aquí. Solución propuesta por Vicky Chijwani:

Configure su servidor para usar la curva elíptica prime256v1. Para ejemplo, en Nginx 1.10 puedes hacer esto configurando ssl_ecdh_curve prime256v1;

 4
Author: Dmitry Maksakov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-24 14:21:51

Estaba usando localhost para la dirección, lo cual obviamente estaba mal. Después de reemplazarlo con la dirección IP del servidor (en la red que es el emulador), funcionó perfectamente.

Editar

En Android Emulator, la dirección de la máquina de desarrollo es 10.0.2.2. Más explicación aquí

 4
Author: Mahmoodvcs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-26 14:14:01

Para Android, es posible que haya perdido el permiso para agregar en AndroidManifest.XML Necesita agregar el siguiente permiso.

<uses-permission android:name="android.permission.INTERNET" /> 
 3
Author: Shashwat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-06 13:49:10

Tengo un problema similar. En mi caso las solicitudes a localhost estaba funcionando y de repente se detuvo. Resultó que el problema era que estaba apagando mi wifi en mi teléfono Android.

 1
Author: Ивайло Янков,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-17 16:26:22

Solo tienes Cambios en Fetch....

fetch('http://facebook.github.io/react-native/movies.json')
    .then((response) => response.json())
    .then((responseJson) => {
        /*return responseJson.movies; */
        alert("result:"+JSON.stringify(responseJson))
        this.setState({
            dataSource:this.state.dataSource.cloneWithRows(responseJson)
        })
     }).catch((error) => {
         console.error(error);
     });
 0
Author: Lavaraju,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-06 05:04:40

Estaba teniendo este problema para Android -

URL - localhost/authToken.json-no funcionó : (

URL-10.106.105.103 / authToken.json-no funcionó : (

URL - http://10.106.105.103/authToken.json - trabajado :): D

Nota: Use ifconfig en Linux o ipconfig en Windows para encontrar la dirección IP de la máquina

 0
Author: Varun Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-14 13:53:04

Ejemplo:

return fetch('http://<your ip>')
  .then((response) => response.json())
  .then((responseJson) => {
    console.log(responseJson)
  })
  .catch((error) => {
    console.error(error);
  });
 -2
Author: Akhila Antony,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-29 05:31:16