2017-06-19 44 views
1

我得到以下错误: 跨域请求被阻止:同源策略不允许读取远程资源http://localhost/index.json。这可以通过将资源移动到相同的域或启用CORS来解决。我得到错误:(交叉原点请求被阻止)angular 2 ionic 2

我在下面给出的是我的代码。 任何机构都可以帮助我解决这个问题。

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

但我使用下面的链接它的到来。 https://conduit.productionready.io/api/profiles/eric。任何机构都可以帮助我使用下面的url http://localhost/index.json –

+0

只需使用Chrome扩展程序:https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi,以便您可以允许CORS。 – Haseoh

+0

确定haseoh,但我正在使用下面的URL它不会来http://myip/get_emp.php?action = getdetails和如何使用本地url? –

回答

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());