XMLHttpRequest can not load in Vue js

0

My app.vue

<template>
    <div id="app">
        <h1>{{ titulo }}</h1>
        <li v-for="usuario of usuarios">

        </li>
    </div>
</template>

<script>
export default {
    name: 'app',
    data() {
        return {

    },
    created() {
        //componente
        let promisse =  this.$http.get('http://swapi.co/api/planets/1/', { headers: { 'Access-Control-Allow-Origin': '*' }});

        console.log(promisse);
        promisse.then(res => alert(res));

    }
}
</script>

My main.js

import Vue from 'vue'
import App from './App.vue'
//importando o vue-rsource
import VueResource from 'vue-resource';

//Usando globalmente o vue-resource
Vue.use(VueResource);

new Vue({
  el: '#app',
  render: h => h(App)
})

You're giving this error

  

XMLHttpRequest can not load link . Request   Access-Control-Allow-Origin header field is not allowed by   Access-Control-Allow-Headers in preflight response. localhost /: 1   Uncaught (in promise) Response {url: " link ",   ok: false, status: 0, statusText: "", headers: Headers, ...}

    
asked by anonymous 28.08.2017 / 20:26

1 answer

1

This type of error occurs because of Cross-origin resource sharing (CORS). Browsers usually make a request is sent OPTIONS type and the server will respond if the type of request you want to make (in your case a GET) is released or not to source (your application).

If the API you are consuming is yours, it will be necessary to configure CORS in it, defining your application as part of the set that can access its resources.

If you have more questions, you can do some research on CORS and its configurations (they vary from platform to platform only in implementation, the conceptual is the same).

    
28.08.2017 / 20:43