我试图阻止从未注册过我的应用程序的用户从数据库中不存在的表单中获取反馈,因此必须注册。 我正在使用fetch与服务器通信。并专门使用passport.js处理身份验证。 在服务器上记录来自端点的响应。 起初我脸红,我认为以下情况适用: 从fetch()返回的Promise不会因HTTP错误状态而拒绝
即使响应是HTTP 404或500,它也会解析
通常(将ok状态设置为false),并且只会在
网络故障或是否有任何阻止请求完成的事情。 但是我注意到我的 因此,我认为我可以轻松创建一个函数来读取响应对象的 但这是正确的方法吗? 这是我在快递服务器上的路线: 这在我的句柄中的fetch调用以我的形式在我的react组件中提交: 最后,浏览器中的网址不应为 更新 我还注意到控制台中的以下内容:type: "basic"
url: "http://localhost:8016/login?error=true"
redirected: true
status: 200
ok: true
statusText: "OK"
ok
状态为true
,尽管url属性包含正确的查询字符串参数,但有错误,即error=true
url
属性并检查query-string
属性是否存在错误。router.route('/login')
.post(passport.authenticate('local', {
successRedirect: '/profile',
failureRedirect: '/login?error=true',
}))
.get((req, res) => res.status(404).send({ error: req.query.error }));
return window.fetch('http://localhost:8016/users/login', {
method: 'POST',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
body: JSON.stringify({ username: username, password: password })
})
.then((response)=>{
console.log('response', response);
if (response.ok) {
const { token } = response.clone();
const loginOptions = {
token,
cookieOptions: { expires: 1 },
callback: () => history.push('/profile')
}
setTimeout(() => {
this.props.logInUser()
login(loginOptions)
}, 5000)
this.setState({
username: '', password: '', formError: false, formSuccess: true, isLoading:false
})
return response.json();
} else if (response.url.split('?').filter(function(el){return !el.indexOf('error')}).includes('error=true')) {
this.setState({
formError: true, formSuccess: false, isLoading: false
});
return;
}
})
.catch(function (error) {
console.log(error);
});
http://localhost:8016/login?error=true
预先感谢您的帮助!SyntaxError: Unexpected token < in JSON at position 0
0 个答案:
没有答案