Angular 我收到错误:(交叉原点请求被阻止)角度2
我得到以下错误: 跨源请求被阻止:同一源策略不允许读取位于的远程资源。这可以通过将资源移动到同一域或启用CORS来解决。 下面是我的代码。 任何人都可以帮我解决这个问题Angular 我收到错误:(交叉原点请求被阻止)角度2,angular,Angular,我得到以下错误: 跨源请求被阻止:同一源策略不允许读取位于的远程资源。这可以通过将资源移动到同一域或启用CORS来解决。 下面是我的代码。 任何人都可以帮我解决这个问题 import { Injectable } from '@angular/core'; import { Http ,Response} from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable() export clas
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());
}
}
您不能将
http.get
url指向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());
您不能将
http.get
url指向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());
但是我正在使用下面的url,它就要来了。任何人都可以帮我使用下面的url,只需使用Chrome扩展名:所以你可以允许CORS.OK haseoh,但我使用下面的url它不会出现,以及如何使用本地url?嗨,haseoh,我使用Chrome advanced rest client下面的请求获取输出,但当我在爱奥尼亚this.http.get中使用它时,无法工作。显示错误“”已阻止跨源请求:同源策略不允许在处读取远程资源。这可以通过将资源移动到同一域或启用CORS来解决。“据我所知,你使用了错误的url。你不能指向localhost,当你从.json获取数据时,你应该像这样指向你的.json目录:
'assets/index.json'
。但是我使用下面的url它即将到来。任何人都可以帮我使用下面的url,只需使用Chrome扩展名:所以你可以允许CORS.OK haseoh,但我使用下面的url它不会出现,以及如何使用本地url?嗨,haseoh,我使用Chrome advanced rest client下面的请求获取输出,但当我在爱奥尼亚this.http.get中使用它时,无法工作。显示错误“”已阻止跨源请求:同源策略不允许在处读取远程资源。这可以通过将资源移动到同一个域或启用CORS来解决。“据我所知,您使用了错误的url。从.json获取数据时,您不能指向localhost。您应该像这样指向.json的目录:'assets/index.json'
。