Search code examples
javaangulareclipseangular8http-error

error:SyntaxError: Unexpected token g in JSON at position 0


This error gives me when I enter letters, if I enter numbers it does not give me. I would like to know how to fix it.

Angular code auth.service.ts

 refactor(data: string): Observable<any>{
        return this.http.post<any>(apiUrl, data)
          .pipe(
            tap(_ => this.log('refactor')),
            catchError(this.handleError('refactor', []))
          );
      }

component.ts

this.anomtextService.refactor($('#file').val().toString())
         .subscribe(res => {

           $('#textAnom').val(res);

         }, (err) => {
           console.log(err);
           alert(err.error);
         });

     }

API REST JAVA

@PostMapping(path = "/rename")
    public String getPathByDoc(@RequestBody String text) {

        return text;

    }

enter image description here

token.interceptor.ts

    import { Injectable } from '@angular/core';
import {
    HttpRequest,
    HttpHandler,
    HttpEvent,
    HttpInterceptor,
    HttpResponse,
    HttpErrorResponse
} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { Router } from '@angular/router';

@Injectable()
export class TokenInterceptor implements HttpInterceptor {

  constructor(private router: Router) {}
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

      const token = localStorage.getItem('token');
      if (token) {
        request = request.clone({
          setHeaders: {
            'Authorization': 'Bearer ' + token
          }
        });
      }
      if (!request.headers.has('Content-Type')) {
        request = request.clone({
          setHeaders: {
            'content-type': 'application/json'
          }
        });
      }
      request = request.clone({
        headers: request.headers.set('Accept', 'application/json')
      });
      return next.handle(request).pipe(
        map((event: HttpEvent<any>) => {
          if (event instanceof HttpResponse) {
            console.log('event--->>>', event);
          }
          return event;
        }),
        catchError((error: HttpErrorResponse) => {
          console.log(error);
          if (error.status === 401) {
            this.router.navigate(['login']);
          }
          if (error.status === 400) {
            alert(error.error);
          }
          return throwError(error);
        }));
  }
}

Maybe the fault is there, I've been pooping things all day.

I would appreciate the help you can give me.

Possible solution they have given me: Angulars HttpClient expects your response type to be json and tries to parse it. You can tell the HttpClient to accept plain text instead.

But I don't know how to solve it


Solution

  • Angulars HttpClient expects your response type to be json and tries to parse it. You can tell the HttpClient to accept plain text instead.

    refactor(data: string): Observable<any>{
        const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
    
        return this.http.post(apiUrl, data,{headers,responseType: 'text'})
              .pipe(
                tap(_ => this.log('refactor')),
                catchError(this.handleError('refactor', []))
              );
    }