Spring MVC 类型'Object'上不存在属性'token'

pftdvrlh  于 2022-11-15  发布在  Spring
关注(0)|答案(1)|浏览(167)

在src/app/login/login.component中出现错误。ts:21:46 -错误TS 2339:类型'Object'上不存在属性'token'。
21本地存储器.setItem(“xAuthToken”,解析令牌);

登录.服务.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

export class LoginService {

  constructor(private http:HttpClient) { }

  sendCredential(username:string,password:string){
    let url="http://localhost:8181/token";
    let encodedCredentials= btoa(username+":"+password);
    let basicHeader="Basic: "+encodedCredentials;
    let header=new HttpHeaders({
      'Content-Type' : 'application/x-www-form-urlencoded',
      'Authorization' : basicHeader
    });
    return this.http.get(url,{headers:header});
  }
}

登录.组件.ts

onSubmit(){
    this.loginService.sendCredential(this.credential.username, this.credential.password).subscribe(
      res => {
          console.log(res);
      localStorage.setItem("xAuthToken", res.token);
          this.loggedIn = true;
          location.reload();
      },
      error => {
          console.log(error);
      }
  );
  }
iq0todco

iq0todco1#

将“res”更改为(res:any)

onSubmit(){
    this.loginService.sendCredential(this.credential.username, this.credential.password).subscribe(
      (res:any) => {
          console.log(res);
      localStorage.setItem("xAuthToken", res.token);
          this.loggedIn = true;
          location.reload();
      },
      error => {
          console.log(error);
      }
  );
  }

相关问题