javascript - 如何避免 Vue.JS/Axion 返回 undefined 而响应代码为 200?
问题描述
我有一个非常简单的 Vue.JS / Axion Get 请求来从 API 获取数据。当我调试响应时,它返回 200 状态代码和预期的数组/对象。
但是 Vue.JS 本身返回[Vue warn]: Error in mounted hook: "ReferenceError: response is not defined"
了为什么我不能使用数据。
这是为什么?
我的Javascript代码:
new Vue({
el: '#app_name',
data() {
return {
info: null
};
},
mounted() {
axios
.get("https://cors-anywhere.herokuapp.com/https://www.api-football.com/demo/api/v2/leagueTable/" + league_id)
.then((response)=> {
this.info = response;
console.log(response);
});
}
}
html部分
<div class="table">
<div><p>Team</p></div>
<div id="app_name"><p>{{ info }}</p></div>
解决方案
你必须在里面添加console.log,然后在得到响应后运行
var app = new Vue({
el: '#app_name',
data: {
info: null
},
mounted(){
let league_id=2;
axios
.get("https://cors-anywhere.herokuapp.com/https://www.api-football.com/demo/api/v2/leagueTable/" + league_id)
.then((response)=> {
this.info = response.data;
console.log(response.data);
});
}
});
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div class="table">
<div><p>Team</p></div>
<div id="app_name">
<p>{{ info }}</p>
</div>
你可以得到你的数据response.data
推荐阅读
- javascript - 如何将特定行从一个电子表格拉到另一个,更新它然后在原始工作表上替换它
- excel - 为什么 CELL("Width", A1) 用“FALSE”填充右侧的单元格?
- node.js - 无法在 firebase 存储桶中创建文件夹 - firebase 管理员
- java - [java]我创建了一个添加类,它可以编译但没有任何东西添加到数组列表中
- angular - 来自html的角度过滤数据
- excel - 如何在 MS Excel 折线图中为每个不同的产品设置不同的水平(类别)标签
- r - R,毫升,毫升。特征重要性总是为每个特征返回 1
- azure-ad-b2c - azure ad b2c 图形 api 权限
- python - 根据列表中的匹配单词将单词大写
- github-api - Github Search API - 排除具有语言的存储库:null