javascript - 如何使用 axios 获取多个 api?
问题描述
我想在同一页面中获取多个 api。使用以下代码,“apione”的所有返回数据都有效,但不知道为什么我从“apitwo”中一无所获。
async created() {
let apione = "https://k67r3w45c4.execute-api.ap-southeast-1.amazonaws.com/TwitterTrends";
let apitwo = "http://ec2-54-179-187-25.ap-southeast-1.compute.amazonaws.com/Test/?param1=HelloHanbinIsFREE"
try {
const res = await axios.get(apione);
this.toptrends1 = res.data[0].Trends;
this.toptrends2 = res.data[1].Trends;
this.toptrends3 = res.data[2].Trends;
this.toptrends4 = res.data[3].Trends;
this.toptrends5 = res.data[4].Trends;
this.tweet1 = res.data[0].TweetVolume;
this.tweet2 = res.data[1].TweetVolume;
this.tweet3 = res.data[2].TweetVolume;
this.tweet4 = res.data[3].TweetVolume;
this.tweet5 = res.data[4].TweetVolume;
var thetime = res.data[0].retrievalDate
thetime = thetime.split("T")[0];
this.time = thetime
// ----------------------------------API 2 -------------------------------------
const res2 = await axios.get(apitwo);
this.trend1fre = res2.data[0].Frequency;
} catch (e) {
console.errpr(e);
}
},
解决方案
在浏览器中打开开发者工具。查看控制台。阅读错误信息。
当我测试你的代码时,我得到:
混合内容:“
https://jsbin.com/?html,js,console
”处的页面是通过 HTTPS 加载的,但请求了不安全的 XMLHttpRequest 端点“http://ec2-54-179-187-25.ap-southeast-1.compute.amazonaws.com/Test/?param1=HelloHanbinIsFREE
”。此请求已被阻止;内容必须通过 HTTPS 提供。
您的一个 API 使用 HTTP,另一个使用 HTTPS。它们都应该使用与加载 HTML 文档时相同的方案(几乎可以肯定是 HTTPS)。
推荐阅读
- php - 为什么 PHP 无法从 Js FormData fetch 中获取 $_POST 数据?
- java - 如何从加密密码中解密密码
- c - 带后缀“f”的双常量用法
- database - 比较 Elastic Search 和 RDS 之间的数据
- visual-studio-code - 如何在 VScode 中预览函数和定义?
- android - 元素未在 Xamarin.Forms 中扩展
- java - 从 DataTable 检索输入
- python - 在 Python 中从数组中格式化日期
- swift - 如果我知道当前日期,如何确定一周中的下几天?
- node.js - 如何在快递中处理角度路线,得到:“res.sendFile 不是函数”