2017-06-19 101 views
0

我想使用休息API獲取數據。我傳遞我的REST API,但它顯示錯誤我得到錯誤,當我使用休息API角2

>  Cross-Origin Request Blocked: The Same Origin Policy disallows reading 
    >  the remote resource at http://ip/JsonServices.php?action=getempdata. 
    >  This can be fixed by moving the resource to the same domain or 
    >  enabling CORS. 

     **Below is my code** 


    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://ip/JsonServices.php?action=getempdata') 

     .map((res:Response) => res.json()); 
     } 

    } 

任何機構幫助我獲得解決上面的錯誤?

回答

1

我認爲這是跨域問題。
如果其他服務器和角度服務器是相同的服務器。 請在下面嘗試。

this.http.get('/JsonServices.php?action=getempdata') 

如果「ip」是外部服務器。 服務器需要回復CORS的響應標頭

我不知道你的服務器代碼。但修復的圖像在下面。

w.Header().Add("Access-Control-Allow-Origin", "*") 
w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept") 
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") 
+0

是的,它是跨域問題。我使用不是我的IP友善地建議上述問題 –

+0

請建議我在哪裏集成上面的標題。我使用PHP作爲服務器代碼,我應該將頭文件集成到php文件中嗎? –

+0

yep.you需要代替php中的abobe代碼,比如'header('Access-Control-Allow-Origin:*');'。然後把它放在JsonServices.php之上 –

相關問題