Vue+Django注册时404错误求助:请求/api/v1/users/返回404
注册请求404错误排查(Vue + Django)
我刚接触Vue一天,目前用Vue做前端、Django做后端开发,部分路由尚未实现。在SignUp.vue中编写了如下代码:
<script> import axios from 'axios' import {toast} from 'bulma-toast' export default{ name : 'SignUp', data(){ return { username :'', password1 : '', password2 : '', errors : [] } }, methods: { submitForm(){ this.errors = [] if(this.username === ''){ this.errors.push('The username is missing') } if(this.password1 === ''){ this.errors.push('The password is too short') } if(this.password1 !== this.password2){ this.errors.push('The passwords are not matching') } if(!this.errors.length){ const formData = { username : this.username, password : this.password1, } axios .post('/api/v1/users/', formData) .then(response => { toast({ message: "Account was created, please log in", type: 'is-success', dismissible:true, pauseOnHover: true, duration: 2000, position: 'bottom-right' }) this.$router.push('/dashboard') return response }) .catch(error =>{ if (error.response){ for (const property in error.response.data){ this.errors.push(`${property}: ${error.response.data[property]}`) } } else if(error.message){ this.errors.push('Something went wrong. Please try again!') } }) } } }, } </script>
每次尝试注册时,都会出现错误:Failed to load resource: the server responded with a status of 404 (Not Found),页面的错误列表显示如下异常内容:
0: < 1: ! 2: D 3: O 4: C 5: T 6: Y 7: P 8: E 9: 10: h 11: t...
已尝试重启Chrome浏览器并清除缓存,但问题仍未解决,恳请帮忙排查。
排查方向及解决建议
- 确认Django后端路由配置:检查Django项目的
urls.py,确保/api/v1/users/的POST路由已正确定义,且对应视图函数支持POST请求。404错误说明后端未匹配到该路由,注意Django路由是否要求末尾斜杠,避免路由规则不匹配。 - 检查axios请求地址:若前后端运行在不同端口/域名下,直接使用相对路径
/api/v1/users/会请求前端所在域名,导致找不到后端接口。需配置axios全局baseURL(如axios.defaults.baseURL = 'http://localhost:8000'),或在Vue项目中配置devServer代理(vue.config.js)转发请求到后端。 - 分析错误返回内容:错误列表中的字符是Django默认404 HTML页面的拆分结果,说明后端返回的是HTML而非预期的JSON错误。这进一步验证路由未匹配成功,后端返回了404页面,而你的catch逻辑遍历了HTML字符串的每个字符,导致错误列表显示单个字符。
- 直接测试后端接口:用Postman或curl工具直接请求后端的
/api/v1/users/地址(使用POST方法),确认接口是否正常响应,排除后端自身问题。
内容的提问来源于stack exchange,提问作者redbeard
相关产品推荐
相关产品推荐

