H. *_*Doe 6 javascript promise
考虑以下代码.这只是一个带axios库的简单http发布请求.
axios.post('http://localhost/users', this.state)
.then(function(response) {
if (response.status == 201) {
browserHistory.push('/features');
}
})
.catch(function(error) {
console.log(error);
})
Run Code Online (Sandbox Code Playgroud)
如果用户向输入输入了错误的数据,则来自服务器的响应保存信息,例如
@标志但不幸的是,如果有一个400 bad request状态,我不知道如何进入该响应.它只是显示错误,但我无法得到响应.
如果响应状态为201,则会正确显示响应.但在这种情况下400,即使我改变条件并添加else if (response.status == 400) { console.log(response) }它也不会显示响应.
任何帮助高度赞赏.
Lan*_*ley 10
只需查看axios文档,看起来响应应该在错误对象中公开(即console.log(error.response)).
有关响应代码超出2xx时提供的不同信息的更多信息:https://github.com/mzabriskie/axios#handling-errors
您需要记录响应的数据:
axios.post('http://localhost/users', this.state)
.then(function(response) {
browserHistory.push('/features');
})
.catch(function(error) {
console.log(error.response.data); // this is the part you need that catches 400 request
});
Run Code Online (Sandbox Code Playgroud)