【问题标题】:my Vuejs app cant send http post with axios我的 Vuejs 应用程序无法使用 axios 发送 http 帖子
【发布时间】:2019-09-13 14:53:32
【问题描述】:

在我的 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)

在 api.js 中包含服务器 URL 和使用 axios 拦截器设置的标头

这里是 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)
   }
}

我尽我所能,但没有成功,我该如何解决这个问题或者我做错了什么

【问题讨论】:

    标签: javascript vue.js post axios


    【解决方案1】:

    好像你没有检索到 ajax 实例:

    return api.post('coursePix', credentials)
    

    代替:

    return api().post('coursePix', credentials)
    

    【讨论】:

      猜你喜欢
      • 2018-03-22
      • 1970-01-01
      • 2022-01-15
      • 2019-12-18
      • 2018-02-15
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      相关资源
      最近更新 更多