当我试图实现只显示一条消息的简单web服务时,总是会遇到这个错误(angular5+springboot)
状态:401,状态文本:“确定”,网址:80/本地主机:你好“;,确定:
错误,}错误:“。。。标题:HttpHeaders
{normalizedNames:Map(0),lazyUpdate:null,lazyInit:}消息:
错误状态:“http401”状态确定“文本:
"80/本地主机:你好";
控制器.java:
@GetMapping("/hello")
public String sayhello(){ return "{\"message\": \"Hello, world!\"}";}
}
你好.component.ts:
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Rx';
import { Adresse } from '../shared/adresse';
@Component({
selector: 'app-forgot', templateUrl: './forgot.component.html',
styleUrls: ['./forgot.component.css']
})
export class ForgotComponent implements OnInit {
message: string;
ip: string = Adresse.ip;
constructor(private http: HttpClient) { }
ngOnInit(): void {
this.http.get(this.ip + '/hello').subscribe(data => {
console.log('DATA', data);
this.message = data['message'];
})
}
}
在尝试构建一个简单的web服务时,遇到Angular5和SpringBoot集成时的401未授权错误。错误信息显示状态为401,请求URL为80/localhost:你好。在Java的Controller.java中,定义了/hello接口返回JSON消息。而在Angular的组件中,使用HttpClient发起GET请求,尝试获取消息。然而,请求未能成功,导致消息无法正常显示。
631

被折叠的 条评论
为什么被折叠?



