2
votes

I'm trying to post some data to a fake API which in this case is jsonPlaceHolder and the idea is that the user can enter a title and content and post it to this API at https://jsonplaceholder.typicode.com/posts using axios by pressing a button but i keep getting the error in the title of this question.

here is the code:

<template>
  <div>
    <h1>it 2020 people</h1>
    <p>type in your title</p>
    <input type="text" v-model="title" />

    <p>type your content</p>
    <textarea cols="30" rows="1" v-model="body"></textarea>

    <h3>your title is {{ title }}</h3>
    <h3>your content is {{ body }}</h3>

    <button v-on:click="post()">post</button>
  </div>
</template>

<script>
import Vue from "vue";
import Axios from "axios";

Vue.use(Axios);
export default {
  components: {},
  data() {
    return {
      title: "",
      body: "",
    };
  },
  methods: {
    post: function () {
      Vue.axios
        .post("https://jsonplaceholder.typicode.com/posts", {
          title: this.title,
          body: this.body,
          userid: 1,
        })
        .then(function (data) {
          return console.log(data);
        });
    },
  },
};

and this is the error that appears before running anything:

Uncaught (in promise) TypeError: Cannot read property 'protocol' of undefined
    at isURLSameOrigin (isURLSameOrigin.js?3934:57)
    at dispatchXhrRequest (xhr.js?b50d:115)
    at new Promise (<anonymous>)
    at xhrAdapter (xhr.js?b50d:13)
    at dispatchRequest (dispatchRequest.js?5270:52)
isURLSameOrigin @ isURLSameOrigin.js?3934:57
dispatchXhrRequest @ xhr.js?b50d:115
xhrAdapter @ xhr.js?b50d:13
dispatchRequest @ dispatchRequest.js?5270:52

and this is the error i get after pressing the button that is supposed to send the data to the API:

[Vue warn]: Error in v-on handler: "TypeError: vue__WEBPACK_IMPORTED_MODULE_0__.default.axios is not a function"

found in

---> <App> at src/App.vue
       <Root>
warn @ vue.runtime.esm.js?2b0e:619
logError @ vue.runtime.esm.js?2b0e:1884
globalHandleError @ vue.runtime.esm.js?2b0e:1879
handleError @ vue.runtime.esm.js?2b0e:1839
invokeWithErrorHandling @ vue.runtime.esm.js?2b0e:1862
invoker @ vue.runtime.esm.js?2b0e:2179
original._wrapper @ vue.runtime.esm.js?2b0e:6917

I really don't have any clue what is the problem, any helpful information is much appreciated. thanks in advance.

1
Vue.use(Axios); 👈 Axios is not a Vue plugin so you cannot use it like this. If you've imported Axios as axios, just use axios.post()Phil
Does this answer your question? axios is not defined in vue js cliPhil
Hmmm I guess you use wrong please check this link github.com/axios/axios/issues/632Anhdevit
@Phil unfortunately every time i use axios.post() it says axios is not definedMilad
@AnhDevit sorry but also didnt work,Milad

1 Answers

2
votes

axios is not a Vue plugin so you cannot use axios like that. Instead, you can create an api service module like this:

//apis.js
import axios from 'axios'

const api  = axios.create({
  baseURL: 'api.domain.com',
  headers: {
    'Content-Type': 'application/json'
  }
})

export default api

and then in the file where you want to make the request

import api from './services/api'
export default {
  methods: {
    async makeRequest() {
      try {
        const res = await api.get('/endpoint')
        console.log(res)  
      } catch (error) {
        console.error(error)
      }
    }
  }
}