2016-11-04 51 views
0

我使用角2開發的應用程序,我發現下面的錯誤,當我嘗試調用API:否「訪問控制允許來源」標頭角2,但捲曲工作正常

XMLHttpRequest無法加載http://localhost:9090/conf。對預檢請求的響應不會通過訪問控制檢查:請求的資源上不存在「訪問控制 - 允許來源」標頭。因此'Origin'http://localhost:5555'不允許訪問。

在這裏,有我的電話的兩個例子:

import { Http, Response } from '@angular/http'; 

// ... 

@Injectable() 
export class ConfService { 

    // ... 

    getConf(): Observable<string[]> { 
    return this.http.get(
         'http:localhost:9090/conf?id=1', 
         { 
         headers: { 
          'Accept': 'application/json;charset=UTF-8' 
         } 
         } 
        ) 
        .map((res: Response) => res.json()) 
        .catch(this.handleError); 
    } 

    setConf(conf: any[]): Observable<string[]> { 
    return this.http.get(
         'http:localhost:9090/conf?id=1', 
         conf, 
         { 
         headers: { 
          'Content-Type': 'application/json;charset=UTF-8' 
         } 
         } 
        ) 
        .map((res: Response) => res.json()) 
        .catch(this.handleError); 
    } 

} 

然而,當我使用捲曲它的作品!

$ curl -XPUT 'localhost:9090/configuration?id=1' \ 
> -H 'Content-Type: application/json' 
> -d '{"conf":1234}' 
Success! 
$ curl -XGET 'localhost:9090/conf?id=1' 
{"conf":1234} 

這裏發生了什麼事?

謝謝

+1

https://chrome.google.com/webstore/detail/cors-toggle/omcncfnpmcabckcddookmnajignpffnh – Lonely

回答

3

這是因爲瀏覽器的安全性,在瀏覽器中的一般CORS問題。您的Web應用程序在localhost:5090上運行,服務器在localhost:9090上運行。瀏覽器將不允許訪問具有不同端口號的另一臺主機。解決方案是處理您的服務器中的CORS(即在您的應用程序運行在localhost:9090)

+0

我的Spring應用程序使用Tomcat和I如果有另一個應用程序(我的Angular客戶端)使用相同的端口,則無法啓動它。 –

+0

當然,他們必須在不同的港口運行。配置你的春天應用程序有CORS標題。 – Tushar

相關問題