react-native - 如何在本机反应中显示网络错误消息
问题描述
我有一个 react native 函数,它使用 fetch 函数来发布一些数据。问题是我无法提醒响应错误消息。我的错误代码是 400。这是我的代码:
fetch(conf.getsignUpURL(), {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
})
.then(function (response) {
if (!response.ok)
{
let error = new Error(response.statusText);
throw error;
}else
{
return response.json();
}
})
.then(async function (data) {
//something
}).catch( (error) => {
alert(error.message);
});
运行后会提示空。
解决方案
fetch(conf.getsignUpURL(), {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
})
.then(function (response) {
// Here I added the alert
alert(JSON.stringify(response))
if (!response.ok)
{
let error = new Error(response.statusText);
throw error;
}else
{
return response.json();
}
})
.then(async function (data) {
// Here I added the alert
alert(JSON.stringify(data))
//something
}).catch( (error) => {
alert(error.message);
});
推荐阅读
- javascript - CHART.JS 如何将 y 轴上的标签偏移/移动/调整到网格线的中间,而不是在网格线上居中?
- c# - 为什么在运行时肯定会失败的 List.Sort 编译?
- html - 如何调整 CSS 以使关闭按钮看起来在正确的位置?
- c# - 如何从存储为 C# 中的对象的对象 [] 进行转换
- javascript - ReactJS/Javascript:componentDidMount() 和渲染
- linux - 如何根据修改时间对Cent OS 7目录中具有特定扩展名的所有文件进行排序?
- javascript - 异步更新 Zabuto 日历
- python-3.x - 为什么在尝试解码我的 base32 字符串时出现“不正确的填充”错误?
- json - 在 Hyperledger Fabric 中存储 JSON 文档
- c# - 如何查询特定类型然后获取基础文档