fetch('http://url.com/api/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(function(data) {
console.log('request succeeded with JSON response', data)
hashHistory.push('/home') //跳转到home
})
.catch(function(error) {
console.log('request failed', error)
})
如上代码,登录信息通过API发送出去后,如果成功需要跳转到/home,但这么写似乎不行,因为试了两下,无论成功失败都会跳转。
应该如何写,才能正确判断登录成功与否做不同的动作呢?
如果失败会返回:
401是在then里面。