我是初级反应前端开发人员。会话过期后,我无法从Chrome中的服务器接收状态401。
长时间停机后,当会话到期时,在firefox中,首先对我的获取请求做出了响应,服务器的响应为401,并且我警告会话已过期。然后,用户将被重定向到身份验证页面(对身份验证页面的200 OK请求)。
但是在chrome中,我没有收到401,并且在网络标签中仅看到对身份验证页面的200 OK请求,此外,我在控制台中收到“ json输入的意外末尾”。我该怎么办?铬如何解决这个问题?浏览器之间这种行为不一致的原因是什么?
我的提取请求:
function getRequestObject(url,callback,options = {}) {
const headers = fetchHeaders;
fetch(API + url,{
headers,credentials: 'same-origin',contentType,})
.then(response => {
if (response.status === 200) {
if (options.raw) {
response.text().then(data => {
callback(data);
});
return;
}
response.json().then(data => {
callback(data.data);
});
} else if (response.status === 401) {
alert('session expired');
window.location.href = '/';
} else if (response.status === 500){
callback({ error: 'Errors.Error500Occurred' });
} else {
response.json().then(data => {
console.error(`ERROR on url ${url}`,data);
if(data.error){
callback({error: data.error });
}else{
callback({error: (data && data.data) || data });
}
});
}
})
.catch(error => {
console.error('error on get request',error);
// callback({error});
alert(i18n.t('common.connection_error'));
window.location.href = '/';
});
}