在尝试实现仅显示msg(angular5 spring boot)的简单Web服务时,我总是遇到此错误
this is the error
:vendor.bundle.js:62557错误HttpErrorResponse {headers:HttpHeaders,status:401,statusText:“OK”,url:“localhost:8080 / hello”;,ok:false,...}错误:“... header :HttpHeaders {normalizedNames:Map(0),lazyUpdate:null,lazyInit:ƒ}消息:“localhost的Http失败响应:8080 / hello:401 OK”名称:“HttpErrorResponse”ok:false状态:401 statusText:“OK” url:“localhost:8080 / hello”;
这是我的简单代码:
controller.java:
@GetMapping("/hello")
public String sayhello(){ return "{\"message\": \"Hello, world!\"}";}
}
hello.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'];
})
}
}