首页 > 解决方案 > 无法使用 Vue.js 完成简单的 GET 请求

问题描述

为了简单起见,我想以与使用 CURL 或 Postman 的 GET 相同的方式发出请求。例如:

curl https://www.google.com

<!doctype html><html itemscope="" ... </body></html>

但是,我无法使用 fetch、axios、request、nee 来做到这一点......

我要做的就是在 Vue.js中进行 GET 调用(到https://www.google.comalert(...); )并弹出结果。

我怎样才能完成如此简单而基本的任务?

标签: restvue.jsget

解决方案


你可以调用fetch一个方法。

Vue.config.devtools = false;
Vue.config.productionTip = false;

var app = new Vue({
  el: '#app',
  data: {
    res: ""
  },
  methods: {
    async get() {
      const url = 'https://jsonplaceholder.typicode.com/todos/1';
      this.res = await fetch(url).then(r => r.json());
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="get">Fetch</button>
  {{ res }}
</div>


推荐阅读