Search code examples
vue.jsaxiosxmlhttprequestvue-cli

Axios in VueJS is mixing the urls. localhost + URL giving error. Should be just the URL


I'have created a project with Vue-cli and the axios get request is not working as spected.

axios.get("​https://pokeapi.co/api/v2/​pokemon")
        .then(response => {
          this.pokemons = response.data;
          console.log(this.pokemons);
        })

The request URL throws http://localhost:8080/%E2%80%8Bhttps://pokeapi.co/api/v2/%E2%80%8Bpokemon .

It should juts be https://pokeapi.co/api/v2/%E2%80%8Bpokemon but that localhost is messing things up.

Please help :(


Solution

  • You have 2 zero width spaces in your string one at the beginning and one before pokemon.
    This may have been caused by pasting code from a word processor.
    The one at the beginning is making your URL be interpreted as a relative URL.
    Remove the spaces and your url should be requested as expected.