javascript - GET api 不能在 react native 中工作,但相同的 api 可以与其他工具一起使用
问题描述
我对第三方 API 有一个奇怪的问题。我正在尝试执行GET
API 的操作,但它会403 error
在 react-native 中引发一个操作,但相同的GET
API 适用于 Postman、Web 浏览器控制台。我无法解决这个问题。
我试图添加credentials: true
, user-agent same as a web browser
,但没有任何效果。
代码是:
axios.get(`https://api.thirdparty.org/username/file/src`, {
headers: {
Authorization: "token"
}
}).then(response => {
console.log(response.data)
return {
status: 200,
data: response.data
}
}).catch(err => {
console.log(err.response)
return {
status: 400,
message: 'Incorrect details'
}
})
提前致谢
解决方案
使用 try and catch
throw 语句允许您创建自定义错误。
从技术上讲,您可以抛出异常(抛出错误)。
getApiData = async () =>{
try {
const url = "https://api.thirdparty.org/username/file/src";
const response = await fetch(url);
if(response.status !== 200){
throw("bad request");
}
const result = await response.json();
return result;
} catch(err){
throw(err)
}
}
推荐阅读
- python - 有人可以解释这个 Google Colab 代码中的不同索引吗?
- sql - 选择查询中带有计数的内部联接
- android - 当视频视图处于全屏状态时,RecyclerView 被重新创建
- chapel - 浮点数的`writeln()`的输出精度
- matplotlib - 为 Jupyter 笔记本全局设置轴标签
- python - 使用子进程调用从 python 调用 fortran 可执行文件
- r - 替换矩阵元素时 foreach 不起作用
- python - 对没有内置或导入函数的数字进行排序以对列表进行排序或计算平均值
- python - 权证的 Pip 安装失败
- netlogo - Netlogo & Levelspace:如何在两个子模型之间传递字符串?