Visual Studio Code 控制台上的 Angular HttpErrorResponse 403

问题描述 投票:0回答:1

我在非独立项目中使用 Angular 17.3.8,并且在 VSCode 控制台中遇到了我无法理解的错误。 我用cookies创建了一个登录系统,它工作得很好。后端向我发送 cookie,Angular 接收它,我可以毫无问题地发出 POST、PUT、GET 和 DELETE HTTP 请求。

当我使用以下代码时,它工作正常,因为我必须按 HTML 按钮才能获取后端信息:

角度分量:

export class AvailableCodesComponent {

  codesThirtyMinutes: Code[] = [];
  codesEightHours: Code[] = [];

  constructor(private codeService: CodeService ) { }


  public getAll(){
    this.codeService.getAllCodes().subscribe(
      (codes: any) => {
        console.log(codes);
      },
      (error: any) => {
        console.log(error);
      }
    );
  }
}

HTML:

  <button (click)="getAll()">GetAll</button>

结果:

enter image description here

另一方面,当我使用 ngOninit 时,我会像这样修改我的代码

export class AvailableCodesComponent implements OnInit{

  codesThirtyMinutes: Code[] = [];
  codesEightHours: Code[] = [];

  constructor(private codeService: CodeService ) { }

  ngOnInit(): void {
    this.getAll();
  }

  public getAll(){
    this.codeService.getAllCodes().subscribe(
      (codes: any) => {
        console.log(codes);
      },
      (error) => {
        console.error(error);
      }
    );
  }

浏览器显示信息正确,但 Vscode 控制台显示此 403 ErrorResponse

Vscode 控制台:

  status: 403,
  statusText: 'Unknown Error',
  url: 'http://localhost:8080/code/getall',
  ok: false,
  name: 'HttpErrorResponse',
  message: 'Http failure response for http://localhost:8080/code/getall: 403 ',
  error: null

enter image description here enter image description here

正如你所见,浏览器说是,而 vscode 控制台说不是,我不知道为什么。 凭证有效,每个请求中都会发送 cookie。 我应该怎么办 ?或者我只是忽略这个 vscode 错误?

angular visual-studio-code get
1个回答
0
投票

您启用了SSR,服务器正在为您执行请求。

您可能想通过将

ssr
中的
angular.json
条目替换为
ssr: false
来禁用 SSR。

© www.soinside.com 2019 - 2024. All rights reserved.