3xCh1_23
3xCh1_23

Reputation: 1499

Failing to accomplish a simple GET request with Vue.js

Just to be simple and up to a point, I want to make a request the same way I would do it with CURL or Postman's GET. For example:

curl https://www.google.com

gives

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

However, I am unable to do it with fetch, axios, request, nee...

All I want to do is to make a GET call (to https://www.google.com) in Vue.js and popup the alert(...); with a result.

How can I accomplish such a simple and a basic task ?

Upvotes: 0

Views: 44

Answers (1)

Pierre Said
Pierre Said

Reputation: 3820

You can call fetch in a method.

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>

Upvotes: 1

Related Questions