简体   繁体   English

错误:语法错误:JSON 中的意外标记 g 在位置 0

[英]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角度代码 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组件.ts

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

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

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

     }

API REST JAVA API REST JAVA

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

        return text;

    }

在此处输入图片说明

token.interceptor.ts 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.他们给我的可能解决方案:Angulars HttpClient 期望您的响应类型为 json 并尝试解析它。 You can tell the HttpClient to accept plain text instead.您可以告诉 HttpClient 接受纯文本。

But I don't know how to solve it但是不知道怎么解决

Angulars HttpClient expects your response type to be json and tries to parse it. Angulars HttpClient期望您的响应类型为 json 并尝试解析它。 You can tell the HttpClient to accept plain text instead.您可以告诉HttpClient接受纯文本。

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', []))
          );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM