如何在 React Native IOS 应用程序中发出 HTTP 请求?
How to Make HTTP Requests In React Native IOS application?
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>
查看更多信息
引用 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.
您的请求是 http
,因此您需要在 ios 应用中将地址添加为应用传输安全例外,或使用 https。
您可以使用正常的获取功能,只是将您的http 主机添加到异常中。
在你的 XCode.
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>
查看更多信息
引用 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.
您的请求是 http
,因此您需要在 ios 应用中将地址添加为应用传输安全例外,或使用 https。
您可以使用正常的获取功能,只是将您的http 主机添加到异常中。 在你的 XCode.