Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

javascript - React Native Post Request via Fetch throws Network Request Failed

I′ve came across the following error. At the moment I developing an Android App with React Native therefore I′m planning to use fetch for doing a post request for me.

fetch("https://XXreachable-domainXX.de/api/test", {
            method: "post",

            body: JSON.stringify({
                param: 'param',
                param1: 'param',

            })
        }
    )
        .then((response) = > response.json()
    )
    .
    then((responseData) = > {
        ToastAndroid.show(
        "Response Body -> " + JSON.stringify(responseData.message), ToastAndroid.SHORT
    )
})
    .
    catch((error) = > {
        console.warn(error);
})
    ;

The app now throws an error:

TypeError: Network request failed

When I change the code to a GET-Request it′s working fine, in the browser with a window.alert() as a return it′s cool and also the chrome extension Postman returns data correctly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Developing with Windows OS/PHP built-in server/react-native Android on device:

  • check server local IP address (ipconfig), e.g. 172.16.0.10
  • in react-native fetch use this URL and proper port (fetch('http://172.16.0.10:8000/api/foo))
  • run PHP built-in server with this specific IP instead of the localhost: php -S 172.16.0.10:8000 ...
  • turn off Windows firewall for the private networks

That fixed the connection problem between Android phone and the local server for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...