我正在验证必须通过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}`);
})
这样做,我只能像这样显示消息
'It works Request failed with status code 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'] 。
关于django - 将服务器错误显示为html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53643799/