试图通过按下按钮使用axios在Vue.js中获得简单的GET响应不起作用

问题描述

我尝试了以下建议:

Print response from http GET request using axios in vue.js?

Vue.js + Axios not assigning response Get response data from axios in vue.js

simple typescript function with axios get call does not work

没有人解决这个问题。

这是我拥有的代码(2020端口,也不适用于8080):

    <template>
    <p>{{ testResponse }}</p>
      <button v-on:click="sendRequest">Get Data</button>
    </template>
    
    <script>
    const axios = require("axios");
    
    export default {
      name: 'Auth',created: function () {
      },data: function () {
        return {
          testResponse:'init',};
      },beforeMount() {},methods: {
        sendRequest() {
          this.getEvents("https://www.google.com");
        },async getEvents(url) {
          axios
              .get(url)
              .then(response => {this.testResponse = response.data})
        },}
    };
    </script>
    </template>

按按钮不执行任何操作。 GET确实发送了,服务器处理了GET,但是没有从中获得数据...我在做什么错了?

编辑:删除垃圾

解决方法

好,我知道了。代码没有错。响应必须为JSON。它适用于“ https://api.npms.io/v2/search?q=vue”,不适用于“ https://www.google.com/”。