【发布时间】:2022-01-14 21:52:32
【问题描述】:
我正在使用 React Native 开发一个移动应用程序,并且在后端有一个 Django 中的 REST API。我想从应用向我的 API 发出 POST 请求。
代码
function sendDpi() {
const requestOptions = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
dpi: imageDpi,
}),
};
fetch('http://127.0.0.1:8000/', requestOptions)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => {
console.log(error);
});
}
当我的应用程序调用sendDpi() 时,我得到[TypeError: Network request failed]。 127.0.0.1:8000 是我的 Django 应用程序运行的地方。我也尝试过使用我的 IP 地址(正如this 回答所指出的那样)。
我还修改了android/app/src/main/AndroidManifest.xml 以包含android:usesCleartextTraffic="true"。
【问题讨论】:
标签: android django react-native fetch-api