【问题标题】:User JsonWebToken in Angular -> Method PostAngular 中的用户 JsonWebToken -> 方法发布
【发布时间】:2021-02-28 09:22:16
【问题描述】:

我在网页端的 node js 中建立了一个网站,我设法使用 axios 登录到该网站。

export const logInFnc = async (email,password)=>{
try{
    const login = await axios({
        method:"POST",
        url:"/api/v1/user/login",
        data:{
             email:email,
             password:password
        }
    });
    if(login.data.status = "success"){
        showAlert('success','You have successfully logged in!!!');
        window.setTimeout(()=>{
            location.assign('/global-post')
        },1500)
    }
}catch(err){
    showAlert('error',`There was a problem,you probably forgot your password or your email address is no longer valid.=>${err}`);
}

}

浏览器中,它和在邮递员中一样正常工作, 但在 angular 中它给了我 400 的错误并且不识别任何东西

【问题讨论】:

  • 但是 Angular 在浏览器中运行。它如何“在浏览器中”而不在 Angular 中工作?
  • 另外,在 Angular 中,您使用 built-in HTTP client 发出 HTTP 请求,而不是使用 Axios 之类的第三方库。

标签: javascript node.js angularjs angular axios


【解决方案1】:

我认为您的 URL 有问题,可能您在 3000 之类的端口中运行 Angular 开发服务器,而 API 在另一个端口中运行。确保这一点。

如果 API 在另一个端口上运行,您应该更改 URL 参数以添加主机名 + 端口,例如 http://localhost:{API_PORT}/api/v1/user/login

【讨论】:

    【解决方案2】:

    我实际上认为您在 URL 中遗漏了一些单词。只需复制请求的 URL(您在邮递员中使用)并在您的 Angular 项目中使用它。或者创建一个环境文件将其保存在那里,然后随时获取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多