我的Vuejs应用无法使用axios发送http发布

时间:2019-04-24 10:25:32

标签: javascript vue.js post axios

在我的Vuejs应用程序中,我试图将HTTP帖子发送到我的服务器,但是我一直在控制台中收到此错误,

TypeError: _api__WEBPACK_IMPORTED_MODULE_0__.default.post is not a function
    at Object.coursePix (services.js?87e7:5)
    at VueComponent._callee$ (testCourse.vue?8c89:98)
    at tryCatch (runtime.js?96cf:45)
    at Generator.invoke [as _invoke] (runtime.js?96cf:271)
    at Generator.prototype.(anonymous function) [as next] (webpack-internal:///./node_modules/regenerator-runtime/runtime.js:97:21)
    at asyncGeneratorStep (asyncToGenerator.js?3b8d:5)
    at _next (asyncToGenerator.js?3b8d:27)
    at eval (asyncToGenerator.js?3b8d:34)
    at new Promise (<anonymous>)
    at new F (_export.js?63b6:36)
包含服务器URL和使用axios拦截器设置的标头的api.js中的

这是api.js

import axios from 'axios'

export default () => {
    const ajax = axios.create({ 
        baseURL: `http://localhost:8081/`
    })

    ajax.CancelToken = axios.CancelToken
    ajax.isCancel = axios.isCancel

    ajax.interceptors.request.use(
        (config) => {
            let token = localStorage.getItem(AUTH_TOKEN)
            if (token) {
                config.headers['Authorization'] = `Bearer ${ token }`
            }
            return config
        },
        (error) => {
            return Promise.reject(error)
        }
    )
    return ajax
}

这是services.js

import api from '@/api'

export default {
    coursePix (credentials) {
        return api.post('coursePix', credentials)
    }
}

和upload.vue

async AddCourse(){
    const formData = new FormData()
    formData.append('file', formData)
    var obj = {
        send: 'send'
    }
    try{                
        await services.coursePix(obj)
    }
    catch(err){
       console.log(err)
   }
}

我尽了一切努力,但是没有用,如何解决这个问题或我做错了什么

1 个答案:

答案 0 :(得分:1)

似乎您没有检索ajax实例:

return api.post('coursePix', credentials)

代替:

return api().post('coursePix', credentials)
相关问题