我尝试发出Http请求并且发生了这个问题,但我之前做过同样的请求并且工作正常,唯一不同的是网址。
服务文件
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');
}
}
组件文件
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)
);
}
}
和HTML5文件
<md-card *ngIf="sistema" >
<md-card-title> {{sistema.Nome}} </md-card-title>
<md-card-content> descrição do sistema </md-card-content>
</md-card>
我是怎么说的,我之前就做过并且有效,但现在不知道为什么。错误就是这个。他们没有找到身份证。
无法加载http://mtz-vweb7/portalEA/api/sistemas/undefined:否&#39; Access-Control-Allow-Origin&#39;标头出现在请求的资源上。起源&#39; http://localhost:4200&#39;因此不允许访问。响应的HTTP状态代码为404。