nate
nate

Reputation: 15

Http failure during parsing for http://localhost:8080/home

I'm trying to connect my angular app to my backend spring-boot app and supposedly I need to make a get request from angular to spring-boot, or at least that's what I've seen people do. I'm assuming this error has something to do with returning JSON when it is an observable thats returned but I don't know how to fix that. But I don't want JSON returned I want the html page from my spring boot app returned.

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

Upvotes: 0

Views: 9143

Answers (1)

Shradha
Shradha

Reputation: 59

Try to add return type as : responseType: 'text' in
return this.http.get(this.baseURL,{responseType: 'text'});

Similar issue : Angular HttpClient "Http failure during parsing"

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

Upvotes: 1

Related Questions