Endi
Endi

Reputation: 13

TypeError: Network request failed android

I have following code in RN:

postToServer(){
  const requestBody = 'pin=1&status=false';
  return fetch('https://192.168.10.200/writeStatus.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body: requestBody
  }).then((response) => response.json())
        .then((responseJson) => {
          console.log(responseJson);
        });
}

also i have following php file:

<?php
  header('Access-Control-Allow-Origin: *');
  $data = json_decode(file_get_contents('php://input'), true);
  print_r($data);
?>

When I run postToServer i receive:

Possible Unhandled Promise Rejection (id: 0): TypeError: Network request failed TypeError: Network request failed at XMLHttpRequest.xhr.onerror (http://192.168.10.248:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false&assetPlugin=P:\sandbox\MojDom1\node_modules\expo\tools\hashAssetFiles:7854:16) at XMLHttpRequest.dispatchEvent (http://192.168.10.248:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false&assetPlugin=P:\sandbox\MojDom1\node_modules\expo\tools\hashAssetFiles:12942:35) ...(some lines here left - i have limit on links) at MessageQueue.callFunctionReturnFlushedQueue (http://192.168.10.248:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false&assetPlugin=P:\sandbox\MojDom1\node_modules\expo\tools\hashAssetFiles:2122:12)

Has anyone some suggestion?

Upvotes: 0

Views: 2067

Answers (1)

Tenten Ponce
Tenten Ponce

Reputation: 2506

Add a catch method for errors:

postToServer(){
  const requestBody = 'pin=1&status=false';
  return fetch('https://192.168.10.200/writeStatus.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body: requestBody
  }).then((response) => console.log(response))
}).catch(e => console.log(e));

Upvotes: 0

Related Questions