8
fetch('http://119.9.52.47:3000/api/countries', {
       method: 'POST',
       headers: { 'Accept': 'application/json','Content-Type': 'application/json'},
   }).then((response) => response.json())
     .then((responseData) => {
           console.log(responseData);
       })

Here is my code . But that's not work.

6
  • edited my answer now check it Commented Mar 8, 2017 at 10:25
  • In which info.plist file i have to add App Transport Security exceptions ? Because more info.plist files in application folder. Commented Mar 8, 2017 at 10:43
  • Search for AppName-Info.plist where AppName is the name of your app. Commented Mar 8, 2017 at 10:54
  • That's Not work @Nitesh Mishra , Network Request Failed issue come. Commented Mar 8, 2017 at 10:59
  • Have you created a webservice? if not then how would you post the data? Commented Mar 8, 2017 at 11:06

4 Answers 4

10

you can try like this for send data to server(POST)

let response = await fetch(
    'http://your_url', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        username: this.state.name,//data which u want to send
        password: this.state.password,
      })
  });
  let responseText = await response.text();
  if (response.status >= 200 && response.status < 300){
    Alert.alert('Server response', responseText)

  }
  else {
    let error = responseText;
    throw error
    //Alert.alert('Login', error)
  }
} catch(errors) {
  Alert.alert('Login', errors)

  Actions.Documents();
}

Edit: As latest iOS sdk enforces connection to be in https protocol instead of http.you can add an exception to your domain inside info.plist file of the Xcode project.

if you want to allow everything write this inside info.plist

<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
    <key>yourdomain.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>

for more info check this out https://stackoverflow.com/a/31623388/7604342

Sign up to request clarification or add additional context in comments.

6 Comments

There was a issue in " let responseText = await response.text(); " @Nitesh Mishra
Error : Syntax Error " await " is the reserved word.
add this code where you want to call this like you can use it inside button press event. let me know if you get any error?
Now also return same Error @ Nitesh Mishra Error : Syntax Error " await " is the reserved word.
Glad it helped :)
|
4

You can use normal fetch function, only add you http host into exception. In your XCode.

enter image description here

Comments

0

Quoting Network docs:

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.

Your request is http, so you need to either add the address as an App Transport Security exception in your ios app, or use https.

3 Comments

How to add the address to App Transport Security exception ? Could you please briefly explain? @Moti Azu
I have the same issue. did you find solution?
Edit in Info.plist
0

i was also facing this issue for the past 2 months. atlast we changed to https .

today too when i checked issue still there but i tried many steps and,
Fixed the issue now.
steps.
remove your existing http request checking code in info.plist
add this

<key>NSAppTransportSecurity</key>
<dict>
     <key>NSExceptionDomains</key>
     <dict>
         <key>NSExceptionAllowsInsecureHTTPLoads</key>
         <true/>
     <key>localhost</key>
     <dict>
         <key>NSExceptionAllowsInsecureHTTPLoads</key>
         <true/>
     </dict>
     </dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
</dict>

then Xcode -> product -> Clean Build folder
then run the app in your iOS device.

Now we can run your http urls in your ios as you needed .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.