我收到以下错误: 阻止跨源请求:同源策略禁止在http://localhost/index.json读取远程资源。这可以通过将资源移动到同一域或启用CORS来解决。
我在下面给出的是我的代码。 任何人帮我解决这个问题。
import { Injectable } from '@angular/core';
import { Http ,Response} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class WebserviceProvider {
constructor(public http: Http) {
console.log('Hello WebserviceProvider Provider');
}
getUser() {
return this.http.get('http://localhost/index.json')
.map((res:Response) => res.json());
}
}
答案 0 :(得分:0)
您无法将http.get
网址指向localhost
。您正在使用.json文件,而不是外部API。这意味着,您必须将.json文件放入assets
文件夹并更改此内容:
return this.http.get('http://localhost/index.json')
.map((res:Response) => res.json());
对此:
return this.http.get('assets/index.json')
.map((res:Response) => res.json());