javascript - react-native axios.get() [错误:请求失败,状态码 404]
问题描述
我正在尝试从此 url ( https://documenter.getpostman.com/view/8854915/SzS7R74q?version=latest ) 实现一个 api。
API 链接- https://api.smartable.ai/coronavirus/news/:location
标头- 订阅密钥:3009d4ccc29e4808af1ccc25c69b4d5d
路径变量- 位置:美国(示例)
我在 Postman Windows 上测试了这个链接。它返回给我一个 json 响应。但是当我尝试使用相同的标头和路径变量对本机反应时,它返回了 404 错误。
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import axios from 'axios';
export default class NationalNews extends React.Component {
state = {
data: ''
}
componentDidMount() {
axios.get('https://api.smartable.ai/coronavirus/news/:location', {
params: {
"location": "US" //it maybe just IN
},
headers: {
'Subscription-Key': '429058f92b3246ae9b8ad1cd88daee46',
},
method: 'get'
})
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
render() {
return (
<View>
<Text></Text>
</View>
)
}
}
请帮忙!
解决方案
您正在使用的兄弟 axios 语法不起作用尝试此代码工作正常
componentDidMount() {
axios.get('https://api.smartable.ai/coronavirus/news/US', {
headers: {
'Subscription-Key': '429058f92b3246ae9b8ad1cd88daee46',
},
})
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
推荐阅读
- ruby - 如何关闭 Unicorn 中的错误详细信息?
- javascript - 如何编写正则表达式来替换某些单词并在 JavaScript 中缺少时添加“”。意外行为
- c# - C# Specflow - 未调用 BeforeScenario 挂钩并且驱动程序为空
- c# - 从 C# 调用 GitVersion
- python - 验证损失低于训练损失,但准确度更差
- r - 根据条件代入或求和
- swift - swift无法将“Int”类型的值分配给“String”类型
- go - 如何使用值初始化映射
- html - 为什么 CSS 中的“margin: 0 auto”+“display:block”命令使元素居中?
- ios - 在 iOS 的移动 Safari 中播放时流式音频失真