【问题标题】:Internal Server Error: /undefined at the end of url for delete request内部服务器错误:删除请求的 url 末尾的 /undefined
【发布时间】:2019-08-31 11:45:17
【问题描述】:

在网址末尾获取undefined/,似乎无法找出原因。
使用 vuejs、vuex 和 bootstrap-vue

` HTML:

     <template slot="time" slot-scope="row">
        <b-button @click="deleteTime(row.value.id)">
          Delete
        </b-button>
      </template>

`

方法: `

    deleteTime (id) {
      this.$store.dispatch('deleteTime', id)
    }

`

Action.vue: `

async deleteTime ({ commit, state }, time) {
    commit('SET_LOADING', true)
    const id = state.route.params.pageId
    await api.deleteTime(id, time)
    commit('SET_LOADING', false)
  }

`

api.js:

`

let deleteTime = '/api/apps/:id/content/time/?id=:timeId'

    async deleteTime (id, time) {
        const url = deleteTime.replace(':id', id).replace(':timeId', time)
        return http.delete(url)
      }

`

我得到了这个http://127.0.0.0:8000/api/apps/1/content/time/?id=10undefined/,但不确定这个未定义来自哪里。

任何帮助将不胜感激。

更新:

http.delete 模块: `

import axios from 'axios'

const http = axios.create({
  xsrfCookieName: 'csrftoken',
  xsrfHeaderName: 'X-CSRFToken'
})

export default {
  async delete (url, id) {
    const response = await http.delete(`${url}${id}/`)
    return response
  }
}

`

谢谢

【问题讨论】:

  • 在方法中放置一个console.log(id),看id是否正确。然后在 Action.vue 和 api.js 中。
  • @echefede 请检查。我已经更新了控制台中方法、操作和 api 的响应
  • 你能分享你的http模块吗?
  • 我假设您要求堆栈跟踪。不幸的是,我不能在这里粘贴它,因为它是一个企业应用程序。但是在“预览”下的“网络”选项卡中查看此输出ValueError at /api/apps/1/content/time/ invalid literal for int() with base 10: '10undefined/' 唯一的问题是我在 url 中附加了未定义。但我似乎确实在 url 中获得了所有必需的值
  • 不,我问的是http.delete() 方法。你用的是 axios 还是什么库?也许你在配置中有一些东西。

标签: vue.js vuex bootstrap-vue


【解决方案1】:

您将带有查询参数的完整网址传递给您的 axios delete。我认为您只需要删除 id 参数即可:

export default {
  async delete (url) {
    const response = await http.delete(`${url}`)
    return response
  }
}

只需删除 id 参数,因为 ${id}/ 的计算结果为 undefined/。您根本没有传递任何 id 参数。

最简单:

只需export http

【讨论】:

  • 感谢您的宝贵时间。我最近也做了这个改变。
猜你喜欢
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 2015-09-20
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多