0

I trying make a Http request and this issue happens, but I just did the same request before and works, the only diference is the url.

the service file

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


@Injectable()
export class SistemaService {
    private _apiUrlSistema = 'http://mtz-vweb7/portalEA/api/sistemas';
    private route_id: string;

    constructor( @Inject(Http) private _http: Http,
        @Inject(ActivatedRoute) private route: ActivatedRoute) {
        this.route_id = route.snapshot.params.id;
    }

    getNomeSistema(): Observable<Response> {
        return this._http.get(this._apiUrlSistema + '/' + this.route_id)
        .map(res => res.json())
        .catch(this.throwError);
    }

    private throwError(Response) {
        return Observable.throw(Response.json().error || 'Server error');
    }
}

component file

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


@Component({
  selector: 'app-titulo-sistema',
  templateUrl: './titulo-sistema.component.html',
  styleUrls: ['./titulo-sistema.component.scss']
})
export class TituloSistemaComponent implements OnInit {
  sistema;
  constructor( @Inject(SistemaService) private sistemaService: SistemaService,
    @Inject(ActivatedRoute) private route: ActivatedRoute) { }

  ngOnInit() {
    this.sistemaService.getNomeSistema().subscribe((sistema) => {
      this.sistema = sistema[0];
    },
      error => alert(error)
    );
  }

}

and the HTML5 file

<md-card *ngIf="sistema" >
  <md-card-title> {{sistema.Nome}} </md-card-title>
  <md-card-content> descrição do sistema </md-card-content>  
</md-card>

How I said, I just did it before and works,but now don't and I don't know why. the error is this. they not found the ID.

Failed to load http://mtz-vweb7/portalEA/api/sistemas/undefined: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 404.

n00dl3
  • 21,213
  • 7
  • 66
  • 76
hudjoubert
  • 143
  • 2
  • 10
  • Possible duplicate of [Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) – n00dl3 Oct 03 '17 at 12:46
  • No, I think is diferent, My API it is making by Java, and I just use before. Why don't works now? I'm beginner. – hudjoubert Oct 03 '17 at 12:50
  • Ok, I will try do this. Can you know why my url show "undefined" at end? – hudjoubert Oct 03 '17 at 13:00
  • Oh didn't note that – n00dl3 Oct 03 '17 at 13:06
  • that might be because there is no CORS handling on your server for 404 that you get this error – n00dl3 Oct 03 '17 at 13:06
  • You should not use `ActivatedRoute` inside service, instead add the id to your method parameters (`getNomeSistema(id:number){...}`). – n00dl3 Oct 03 '17 at 13:08
  • I just use to take activated route and display json information from this respective id. is it wrong? – hudjoubert Oct 03 '17 at 13:16
  • I try do this, but don't work. Now had this problem. **Failed to load http://mtz-vweb7/portalEA/api/areas: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'http://localhost:4200' is therefore not allowed access.** @n00dl3 – hudjoubert Oct 04 '17 at 14:49

0 Answers0