gpt4 book ai didi

react-native - 如何在 React Native IOS 应用程序中发出 HTTP 请求?

转载 作者:行者123 更新时间:2023-12-03 09:24:41 26 4
gpt4 key购买 nike

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 协议(protocol)而不是 http。您可以在 Xcode 项目的 info.plist 文件中向您的域添加一个异常(exception)。
如果你想让一切都写在 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

关于react-native - 如何在 React Native IOS 应用程序中发出 HTTP 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com