API请求出现401状态错误

时间:2018-07-31 09:05:47

标签: angular spring-boot

当尝试实现仅显示msg(angular5 + spring boot)的简单Web服务时,总是会出现此错误

这是错误

  

:vendor.bundle.js:62557错误HttpErrorResponse {标头:HttpHeaders,   状态:401,statusText:“确定”,网址:“ localhost:8080 / hello”;确定:   错误,…}错误:“ ...标头:HttpHeaders   {normalizedNames:Map(0),lazyUpdate:null,lazyInit:ƒ}消息:   “对localhost:8080 / hello:Http OK的HTTP故障响应”名称:   “ HttpErrorResponse”确定:错误状态:401 statusText:“确定”网址:   “本地主机: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'];
    })
  }
}

0 个答案:

没有答案