【发布时间】:2021-07-14 09:16:09
【问题描述】:
我正在学习如何将 API 与 VueJS 一起使用。我在 Rapidapi 上获取了开源 API 数据。
我通过输入链接https://covid-19-data.p.rapidapi.com/report/country/name?name=Italy&date=2020-04-01 尝试使用 Postman,并得到了响应。
但是在VueJS中尝试时,数据没有出现。我的循环中是否有任何遗漏?
这是我的代码的托管代码框:https://codesandbox.io/s/nice-rain-45j6y
<template>
<div v-for="post in posts" :key="post.id">
{{ post.country }}
{{ post.confirmed }}
</div>
</template>
<script>
export default {
data() {
return {}
},
computed: {
posts() {
return this.$store.state.posts
}
},
mounted() {
this.$store.dispatch("loadPosts");
}
};
</script>
import axios from 'axios'
const options = {
method: 'GET',
url: 'https://covid-19-data.p.rapidapi.com/country',
params: {name: 'italy'},
headers: {
'x-rapidapi-key': '1031599022msh37c84da8c4e9b0ap1047d6jsn4d6475b64dc7',
'x-rapidapi-host': 'covid-19-data.p.rapidapi.com'
}
};
const covid = {
state: () => ({
posts: []
}),
mutations:{
setPosts(state, components){
state.posts = posts
}
},
actions: {
loadPosts({ commit }) {
axios.request(options)
.then(response => {
commit('setPosts', response.data)
console.log(response.data)
})
.catch(function (error) {
console.error(error);
});
}
},
}
export default covid;
我的console.log(response.data) 得到的回复如下
【问题讨论】:
-
当我在 Postman 上试用它时,我也收到了回复……这是一个带有消息
"message": "Invalid API key. Go to https://docs.rapidapi.com/docs/keys for more info."的 401。我不明白你为什么对 axios 给你一个类似的错误感到惊讶。 -
我应该把 api 密钥放在哪里? @昆汀
-
我不知道。尝试阅读该服务的文档。
-
您可能需要某种身份验证,因此是 401。
-
我已经更改了上面的代码,并在控制台中得到了响应。但是为什么在vue js模板中没有出现@kissu
标签: javascript vue.js axios vuejs3 rapidapi