如何解决 Angular2 + Lite-Server 中的 "Access-Control-Allow-Origin" 问题?
How to solve "Access-Control-Allow-Origin" issue in Angular2 + Lite-Server?
我有一个 Angular 2 应用程序从外部请求数据 API。
我无法更改 API 代码,但我可以更改 TypeScript 和 Lite-Server 配置。
错误:
XMLHttpRequest 无法加载 http://api.zanox.com/.... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' 因此不允许访问。
我已经看过很多关于 CORS 的内容,但我不知道如何将其应用到我的代码中。解决该问题的最简单方法是什么?
我的服务:
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Page } from './page';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class ProductService {
private urlPage = 'http://api.zanox.com/json/...';
constructor(private http: Http) { }
getPage(): Observable<Page> {
return this.http.get(this.urlPage).map(this.extractData).catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || {};
}
private handleError(error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg);
return Observable.throw(errMsg);
}
}
我的组件:
import { Component, OnInit } from '@angular/core';
import { ProductService } from './product/productService';
import { Page } from './product/page';
@Component({
templateUrl: 'app/app.product.html',
selector: 'product-app',
providers: [ProductService]
})
export class AppProduct implements OnInit {
private errorMessage: string;
page: any;
constructor(
private productService: ProductService) {
}
ngOnInit() {
this.getPage();
}
getPage() {
this.productService.getPage().subscribe(
page => this.page = page,
error => this.errorMessage = <any>error
)
}
}
我可以使用 JSONP 解决问题:
app.module.ts:
import { JsonpModule } from '@angular/http';
@NgModule({
imports: [JsonpModule]
})
productService.ts:
import {Jsonp} from '@angular/http';
private urlPage = 'http://api.zanox.com/json/...&callback=JSONP_CALLBACK';
constructor(private _jsonp: Jsonp) {}
getPage(): Observable<Page> {
return this._jsonp.get(this.urlPage).map(this.extractData).catch(this.handleError);
}
我有一个 Angular 2 应用程序从外部请求数据 API。
我无法更改 API 代码,但我可以更改 TypeScript 和 Lite-Server 配置。
错误: XMLHttpRequest 无法加载 http://api.zanox.com/.... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' 因此不允许访问。
我已经看过很多关于 CORS 的内容,但我不知道如何将其应用到我的代码中。解决该问题的最简单方法是什么?
我的服务:
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Page } from './page';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class ProductService {
private urlPage = 'http://api.zanox.com/json/...';
constructor(private http: Http) { }
getPage(): Observable<Page> {
return this.http.get(this.urlPage).map(this.extractData).catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || {};
}
private handleError(error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg);
return Observable.throw(errMsg);
}
}
我的组件:
import { Component, OnInit } from '@angular/core';
import { ProductService } from './product/productService';
import { Page } from './product/page';
@Component({
templateUrl: 'app/app.product.html',
selector: 'product-app',
providers: [ProductService]
})
export class AppProduct implements OnInit {
private errorMessage: string;
page: any;
constructor(
private productService: ProductService) {
}
ngOnInit() {
this.getPage();
}
getPage() {
this.productService.getPage().subscribe(
page => this.page = page,
error => this.errorMessage = <any>error
)
}
}
我可以使用 JSONP 解决问题:
app.module.ts:
import { JsonpModule } from '@angular/http';
@NgModule({
imports: [JsonpModule]
})
productService.ts:
import {Jsonp} from '@angular/http';
private urlPage = 'http://api.zanox.com/json/...&callback=JSONP_CALLBACK';
constructor(private _jsonp: Jsonp) {}
getPage(): Observable<Page> {
return this._jsonp.get(this.urlPage).map(this.extractData).catch(this.handleError);
}