简体   繁体   English

如何使用 axios 获取多个 api?

[英]How to get multiple api with axios?

I want to get multiple api in the same page.我想在同一页面中获得多个 api。 With the following code, all the return data from "apione" is work, but not sure why I get nothing from the "apitwo".使用以下代码,“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);
    }
  },

Open the Developer Tools in your browser.在浏览器中打开开发者工具。 Look at the Console.查看控制台。 Read the error messages .阅读错误消息

When I test your code, I get:当我测试你的代码时,我得到:

Mixed Content: The page at ' https://jsbin.com/?html,js,console ' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint ' http://ec2-54-179-187-25.ap-southeast-1.compute.amazonaws.com/Test/?param1=HelloHanbinIsFREE '.混合内容:“ https://jsbin.com/?html,js,console ”页面已通过 HTTPS 加载,但请求了不安全的 XMLHttpRequest 端点“ http://ec2-54-179-187-25.ap-southeast-1.compute.amazonaws.com/Test/?param1=HelloHanbinIsFREE '。 This request has been blocked;此请求已被阻止; the content must be served over HTTPS.内容必须通过 HTTPS 提供。

One of your APIs uses HTTP the other uses HTTPS.您的 API 之一使用 HTTP,另一个使用 HTTPS。 They should both use the same scheme as you used to load the HTML document (and that should almost certainly be HTTPS).它们都应该使用与您用来加载 HTML 文档相同的方案(并且几乎肯定应该是 HTTPS)。

the problem with the response from apitwo is just the lack of the necessary Access-Control-Allow-Origin header, you can still get things to work—by making the request through a CORS proxy.来自apitwo的响应的问题只是缺少必要的Access-Control-Allow-Origin标头,您仍然可以通过 CORS 代理发出请求来使事情工作。 replace below替换下面

const res2 = await axios.get(apitwo);

with

const res2 = await axios.get('https://cors-anywhere.herokuapp.com/'+apitwo);

for further research on the problem go to this stackover flow answer要对该问题进行进一步研究,请转到此stackover flow answer

尝试axios.all([apione, apitwo])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM