django - 在 html 中显示服务器错误
问题描述
我正在验证我必须通过 ajax 发送的表单,我正在使用 axios 来执行此操作,但我现在想知道是否有办法将我从服务器收到的错误显示到我的 html 中,这些是我的错误想展示
我在服务器端使用 django rest-framework 这是我的代码
axios({
method: 'post',
url: `${baseURL}api/users/`,
contentType: 'application/json',
data : {} // I send the inputs data here
})
.then(response => {
window.location.href = '/success.html';
console.log(response)
})
.catch(err => {
console.log(`It works ${err.message}`);
})
这样做,我只能显示这样的消息
'它工作请求失败,状态码为 400'
解决方案
尝试类似:
var errors = {}
...
.then(response => {
window.location.href = '/success.html';
console.log(response)
errors = Object.assign({})
})
.catch(err => {
errors = Object.assign({}, err.data['error'])
console.log(`It works ${err.message}`);
})
然后,您可以使用此变量在 html 中显示错误,例如errors['address']。
推荐阅读
- sql - Oracle DB:为什么稍微重写这个查询会使它快很多倍?
- c# - 使用不同的属性名称绑定到 POCO 模型
- java - 如何在 Java API Elasticsearch-2.4.4 中的存在查询中使用多个字段?
- vba - 如何通过 VBA 在 Outlook 中发送多个收件人?
- c# - 用于将电子邮件地址与域匹配的正则表达式
- hibernate - Hibernate:多对多,带有额外的列和遗产
- javascript - 将字符串发送到 UnityWebGL jslib
- neo4j - 如何处理neo4j中两个节点之间的多条路径?
- php - jQuery 与数据库列一起发送附加数据?
- c# - 我可以以某种方式获取表值参数并将其作为 IQueryable 返回吗