Как я могу поместить в массив этот acces_token из предварительного просмотра? - PullRequest
0 голосов
/ 07 мая 2019

I Я пытаюсь обработать токен из моего бэкэнда, но я получаю токен в тексте ошибки. Я пробовал этот курс https://www.udemy.com/angular-laravel-single-page-app-with-authentication-and-password-reset/learn/v4/t/lecture/10341584?start=0 но не помог мне, и теперь я застрял здесь

import { Component, OnInit } from '@angular/core';
import {JarwisService} from '../../Services/jarwis.service';
import {TokenService} from '../../Services/token.service';
import {HttpClient} from '@angular/common/http';


@Component({
  selector: 'app-singin',
  templateUrl: './singin.component.html',
  styleUrls: ['./singin.component.css']
})
export class SinginComponent implements OnInit {

  constructor(
    private Jarwis: JarwisService,
    // private Token: TokenService
    private http: HttpClient
  ) {}

  public form = {
    email: null,
    password: null
  };

  ngOnInit() {
  }

  onSubmit() {
   this.http.post<any>(`http://127.0.0.1:8000/api/login`, this.form).subscribe(
      data => console.log(data)
    );

...

И я получаю в консоли это

core.js:1673 ERROR 
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://127.0.0.1:8000/api/login", ok: false, …}
error:
error: SyntaxError: Unexpected token h in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:7458:51) at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:12332:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:36916:33) at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:12331:60) at Zone../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:4200/polyfills.js:12104:47) at ZoneTask../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:12407:34) at invokeTask (http://localhost:4200/polyfills.js:13653:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:13690:21)
text: "http://127.0.0.1:8000/{"success":true,"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xMjcuMC4w

1 Ответ

1 голос
/ 07 мая 2019

Вы не получаете действительный JSON с сервера, вы можете видеть, что запрос был успешно выполнен, но ответом является строка, содержащая «http» в начале:

http://127.0.0.1:8000/{"success":tr...

Вот почему вы видите "ч "в Unexpected token h in JSON at position 0 at JSON.parse.

Ошибка воспроизведения:

var invalidJson = 'http://127.0.0.1:8000/{"success":true,"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xMjcuMC4w';

var validJson = '{"obeject":{"success":true,"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xMjcuMC4w"}}';

//invalid json
try {
  console.log(JSON.parse(invalidJson))
} catch (e) {
  //see console
  console.log("Error is: " + e);
}

//valid json
try {
  console.log(JSON.parse(validJson))
} catch (e) {
  //see console
  console.log("Error is: " + e);
}
...