解析http:// localhost:8080 / home期间的Http失败

时间:2020-06-23 17:59:46

标签: angular spring-boot

我正在尝试将我的angular应用程序连接到后端spring-boot应用程序,并且据说我需要从angular到spring-boot发出get请求,或者至少这是我所看到的。我假设此错误与返回的JSON有关,但返回的值可以观察到,但我不知道如何解决。但是我不希望返回JSON,我希望返回我的spring boot应用程序中的html页面。

SecurityConfig:

package com.prototype.prototype.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {

        http
                .antMatcher("/**").authorizeRequests()   
                .antMatchers(new String[]{"/home", "/not-restricted", "/css/**"}).permitAll() 
                .anyRequest().authenticated()
                .and()
                .oauth2Login();
    }
}

HomeController:

package com.prototype.prototype.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController
{
    @CrossOrigin("http://localhost:4200")
    @GetMapping("/home")
    public String Login() {
        return "index";
    }
   
    @GetMapping("/restricted")
    public String restricted() {
        return "final";
    }
}

rest.sevice.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class RESTService {

  private baseURL = 'http://localhost:8080/home';

  constructor(private http: HttpClient) { }

  checkUsers(): Observable<any> 
  {

  return this.http.get(this.baseURL);
  }
}

servicecomponent.ts:

constructor(private rest: RESTService) { }
ngOnInit(): void 
  {
   
    this.rest.checkUsers().subscribe();
    
  }

image of error

1 个答案:

答案 0 :(得分:1)

尝试将返回类型添加为:responseType:'text'in
返回this.http.get(this.baseURL,{responseType:'text'});

类似的问题: Angular HttpClient "Http failure during parsing"

Angular 6: How to set response type as text while making http call