Angular HTTP 302 错误:无法从 Spring Boot API 检索对象

Angular HTTP 302 Error: Trouble retrieving object from Spring Boot API

提问人:ig2jmu 提问时间:11/17/2023 更新时间:11/17/2023 访问量:11

问:

我目前正在开发一个 Angular 应用程序,我试图从 URL 中检索组名,其格式为“localhost:8080/groups/g/example”。在我的 Angular app.routing-module.ts文件中,我配置了参数“example”作为 URL 的一部分。我的目标是根据此参数从Spring Boot后端获取相应的组对象。但是,我在此过程中遇到了一个错误。请务必注意,使用 Postman 进行测试时,此功能可以顺利运行。

ShowGroupPost组件:

import { Component, OnInit } from '@angular/core';
import { Group } from '../module/Group';
import { ActivatedRoute } from '@angular/router';
import { GroupService } from '../services/group-service.service';

@Component({
  selector: 'app-show-group-post',
  templateUrl: './show-group-post.component.html',
  styleUrls: ['./show-group-post.component.css']
})
export class ShowGroupPostComponent implements OnInit {
  group:Group = new Group("", "");

  constructor(private route:ActivatedRoute,
              private groupService:GroupService){}

  ngOnInit(): void {
    let groupName = this.route.snapshot.paramMap.get('groupName');
    this.groupService.getGroupByName(groupName).subscribe((group) => this.group = group);
  }
}

集团服务:

import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Observable, of} from "rxjs";
import { Group } from '../module/Group';

@Injectable({
  providedIn: 'root'
})
export class GroupService {
  BASE_URL : string = "http://localhost:8080";
  url : string = "";
  constructor(private http: HttpClient) { }

  public getGroupByName(name:String | null):Observable<Group>{

    if(name === null)
      return of(new Group("",""));

    this.url = this.BASE_URL + "/groups/g/" + name;

    return this.http.get<Group>(this.url);
  }
}

错误:

HttpErrorResponse {headers: HttpHeaders, status: 302, statusText: 'OK', url:'http://localhost:8080/groups/g/example', ok: false, …}
error: {name: 'example', description: 'exampleDesc'}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for http://localhost:8080/groups/g/example: 302 OK"
name: "HttpErrorResponse"
ok: false
status: 302
statusText: "OK"
url: "http://localhost:8080/groups/g/example"
[[Prototype]] : HttpResponseBase

尽管使用 Postman 进行了成功的尝试,但在尝试从 Angular 应用程序中的 Spring Boot 后端检索组对象时,我遇到了此错误。我正在寻求有关此问题潜在解决方案的指导或见解。

Angular TypeScript 休息

评论


答: 暂无答案