react-native – 如何在React Native IOS应用程序中发出HTTP请求
发布时间:2020-12-15 05:05:11 所属栏目:百科 来源:网络整理
导读: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); }) 这是我
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); }) 这是我的代码.但那不行.
您可以尝试将此数据发送到服务器(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(); } 编辑:最新的iOS sdk强制连接为https协议而不是http.you可以在Xcode项目的info.plist文件中为您的域添加例外. 如果你想允许一切写在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> 有关更多信息,请查看https://stackoverflow.com/a/31623388/7604342 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |