【问题标题】:GET http://localhost:3000/user/authenticated 401 (Unauthorized) how to bypass this error for certain requestsGET http://localhost:3000/user/authenticated 401 (Unauthorized) 如何绕过某些请求的这个错误
【发布时间】:2021-07-11 13:02:03
【问题描述】:

GET http://localhost:3000/user/authenticated 401 (Unauthorized) 对于某些不需要用户认证的请求,如何绕过这个错误。例如,电子商务商店能够向该网站的任何访问者展示所有产品,但卖家需要登录。那么我如何用我在下面创建的代码绕过这个呢?对不起,如果这解释得不好,我是后端和用户身份验证的新手。

请注意,我在获取数据方面没有问题,该过程运行良好。它只是出现错误,我想删除它。

以下是我如何设置用户身份验证错误源于 iaAuthenticated 部分,我将突出显示位置。

export default {
    login : user => {
        return fetch('/user/login', {
            method : "post",
            body : JSON.stringify(user),
            headers: {
                'Content-Type' : 'application/json'
            }
        }).then(res=>{
            if(res.status !== 401)
                return res.json().then(data => data);
            else
                return {isAuthenticated : false, user: {username : ""}}
        })
    },
    
    register : user => {
        return fetch('/user/register', {
            method : "post",
            body : JSON.stringify(user),
            headers: {
                'Content-Type' : 'application/json'
            }
        }).then(res => res.json())
        .then(data => data);
    },

    logout : ()=> {
        return fetch('/user/logout')
        .then(res => res.json())
        .then(data => data);
    },
    isAuthenticated : () => {
        (error appears from this line)return fetch ('/user/authenticated')
        .then(res=>{
            if(res.status !== 401)
                return res.json().then(data => data);
            else
                return {isAuthenticated : false, user: {username : ""}}
        })
    }
}

【问题讨论】:

  • 能否显示后端代码(路由)?

标签: node.js reactjs express


【解决方案1】:

有两件事可以做。

  1. 将文件从目录移动到上面的不同目录。 如果这使页面正常工作,那么文件就是问题
  2. 尝试查看您对 apache2.conf 或 .conf 或 .htaccess 进行的任何配置更改。 如果您更改了设置并且该设置有效,您可以再次尝试查看您的文件是否有效。

【讨论】:

    猜你喜欢
    • 2020-10-20
    • 1970-01-01
    • 2019-07-07
    • 2021-05-19
    • 2017-12-20
    • 1970-01-01
    • 2021-12-31
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多