Javascript 未授权:错误401 passport jwt

Javascript 未授权:错误401 passport jwt,javascript,node.js,authentication,Javascript,Node.js,Authentication,passport.js Authentication.js routes.js 服务等级 .ts 获取数据 getContacts() { console.log('Inside getContacts() of service class'); return new Promise((resolve, reject) => { let headers = new Headers(); headers.append('Authorization',

passport.js

Authentication.js

routes.js

服务等级 .ts

获取数据

getContacts() {
    console.log('Inside getContacts() of service class');
    return new Promise((resolve, reject) => {
      let headers = new Headers();
      headers.append('Authorization', this.auth.token);
      console.log('inside promise');

      this.http.get('http://localhost:8080/api/contacts/', { headers: headers }).map(res =>
        res.json())
        .subscribe(data => {
          resolve(data);
          console.log('inside resolve of service class', data);
        },
        (err) => {
          reject(err);
        });
    });
  }
我这里不使用授权,只使用身份验证。当从服务类调用get post时,它会显示unauthorized。请帮助

在Authentication.js文件中,我使用了Token:跟随您的ans到某个帖子,我删除了“JWT”。但随后显示的错误太多


请帮忙

在您的headers.append(位于您的服务文件中)请求授权时,请将其切换到身份验证,看看是否有效

您可以尝试以下方法:

headers.append('Authorization', 'JWT ' + this.auth.token);
headers.append('Authorization', 'Bearer ${this.auth.token}');

您的日志中显示了什么?401..未经授权我可以在代码中看到一些console.log。它们中没有一个从未被调用过?尝试将我的答案和上面的答案结合起来,使其看起来像是标题。附加'Authorization','JWT',+this.auth.token;只是想知道,您是否通过mongo shell为您当前使用的数据库设置了用户和pwd?是的,当然,我已经设置了用户/密码,即使我能够登录,但当我登录页面时,会给出未经授权的。在我的第三个回答中,意思是说身份验证btw未授权,但是,如果您登录到MongoShell,那么passport.js中的这两个if语句又有什么关系呢?你应该使用else if^如果你真的想得到帮助,你应该详细说明你的问题。例如:console.log输出和来自标题的转储。
getContacts() {
    console.log('Inside getContacts() of service class');
    return new Promise((resolve, reject) => {
      let headers = new Headers();
      headers.append('Authorization', this.auth.token);
      console.log('inside promise');

      this.http.get('http://localhost:8080/api/contacts/', { headers: headers }).map(res =>
        res.json())
        .subscribe(data => {
          resolve(data);
          console.log('inside resolve of service class', data);
        },
        (err) => {
          reject(err);
        });
    });
  }
headers.append('Authorization', 'JWT ' + this.auth.token);
headers.append('Authorization', 'Bearer ${this.auth.token}');