JSON输入Angular 2的意外结束

时间:2017-10-05 13:38:50

标签: angular angular2-services angular-http

我在Angular 2中有一个应用程序。我发出了一个http请求但是没有工作。

这是错误

zone.js:2263获取http://mtz-vweb7/portalEA/api/sistemas/undefined/tecnologias 404(未找到) core.es5.js:1020 ERROR SyntaxError:JSON输入的意外结束

我的服务

import { ActivatedRoute, Params } from '@angular/router';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Injectable, Inject } from '@angular/core';

@Injectable()
export class TecnologiaService {

    private _apiUrlSistema = 'http://mtz-vweb7/portalEA/api/sistemas';
    private route_id: string;

    constructor( @Inject(Http) private _http: Http,
        @Inject(ActivatedRoute) private routeTecnologia: ActivatedRoute) {
        console.log('rota no servico', routeTecnologia.snapshot.params.id);
        this.route_id = routeTecnologia.snapshot.params.id;
    }

    getTecnologias(): Observable<Response> {
        return this._http.get(this._apiUrlSistema + '/' + this.route_id + '/tecnologias')
            .map(res => res.json())
            .catch(this.throwError);
    }
    id() {
        return this.route_id;
    }
    private throwError(Response) {
        return Observable.throw(Response.json().error || 'Server error');
    }
}

我的组件

import { TecnologiaService } from './tecnologia.service';
import { ActivatedRoute, Params } from '@angular/router';
import { Component, OnInit, Inject } from '@angular/core';


@Component({
  selector: 'app-tecnologias',
  templateUrl: './tecnologias.component.html',
  styleUrls: ['./tecnologias.component.scss']
})
export class TecnologiasComponent implements OnInit {
  listaTecnologias;
  private idDaRota;
  constructor( @Inject(TecnologiaService) private sistemaService: TecnologiaService,
    @Inject(ActivatedRoute) private route: ActivatedRoute) {
      console.log('rota no componente', this.route);
    }

  ngOnInit() {
    this.sistemaService.getTecnologias()
      .subscribe(tecnologia => this.listaTecnologias = tecnologia[0]);

    this.idDaRota = this.sistemaService.id;
  }
}

我把日志显示给我激活的路线然后返回

    snapshot
:
ActivatedRouteSnapshot
children
:
(...)
component
:
ƒ DetalheSistemaComponent(service, route)
data
:
{}
firstChild
:
(...)
fragment
:
undefined
outlet
:
"primary"
paramMap
:
(...)
params
:
id
:
"38011"

因此我接受了身份证明但未明确地告诉我,我不知道为什么。

0 个答案:

没有答案