2017-04-07 72 views
0

我在React Native中有登录功能,我尝试获取数据。功能如下:Fetch在React Native中不起作用ios

onLogin: function() { // 
     const self = this; 
     self.setState({modalVisible: true, modalMessage: 'Signing in'}); 
     fetch('http://psa.autralis.com/manager/api/v1/obtain-auth-token/', { 
      method: 'POST', 
      headers: { 
       'Content-Type': 'application/json', 
      }, 
      body: JSON.stringify({ 
       username: self.state.username, 
       password: self.state.password, 
      }) 
     }) 
      .then(response => response.json()) 
      .then(data => console.log(data)) 
      .catch((error) => { 
       console.warn(error); 
      }); 
    }, 

但由于某些原因,它不上IOS工作。我得到一个错误:

enter image description here

在Android上它的工作原理,和我一起邮差测试它还有也适用。

有什么想法?

回答

1

这个问题的原因是:iOS默认不允许http请求,只有https。如果你需要使用http做这个,只需更改你的info.plist:

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict> 
+0

我解决了它已经和我添加的溶液。但我会接受你的回答。 – Boky

0

万一别人需要它。我加入

<key>NSAllowsArbitraryLoads</key> 
<true/> 

Info.plist文件位于ios文件夹内的项目解决了这个问题。

因此我改变

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

<key>NSAppTransportSecurity</key> 
    <dict> 
     <key>NSAllowsArbitraryLoads</key> 
     <true/> 
     <key>NSExceptionDomains</key> 
     <dict> 
     <key>localhost</key> 
     <dict> 
      <key>NSExceptionAllowsInsecureHTTPLoads</key> 
      <true/> 
     </dict> 
     </dict> 
    </dict> 
相关问题