2017-07-14 162 views
0

我目前正在開發一個項目,我需要訪問連接到確定的Rabbitmq的所有隊列。爲此,我試圖使用rabbitmq的RESTful服務獲取這些信息。我嘗試使用POSTman做一些例子,例如發送一個請求到localhost:15672/api/connections與guest:guest作爲用戶名和密碼,並且一切正常;然而,當我試圖用我的項目發送相同的請求,對角以下錯誤消息出現:在Angular2中使用RabbitMQ RESTful服務

zone.js:2177 XMLHttpRequest cannot load localhost:15672/api/overview. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

我做了這個錯誤的一些研究,所有的解決方案,我能找到的建議,我應該讓我的瀏覽器在Chrome上製作CORS請求,但是這種解決方案需要我更改每個可以運行我的項目的瀏覽器,這將不會有效。任何人都知道我可以使用Angular 2應用程序獲得Rabbitmq信息的另一種方式?

在此先感謝!

編輯:

而且,如果我添加的 「http://」 的請求開始,該錯誤信息更改爲:

XMLHttpRequest cannot load http://localhost:15672/api/overview . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:4200 ' is therefore not allowed access. The response had HTTP status code 405.

這裏是我的代碼看起來像:

@Injectable() 
export class RabbitHttp { 

protected headers: Headers; 

constructor(private http: Http, private router: Router, 
    public globalService: GlobalService 
) { 
} 
    public get(url: string, parameters: any): Promise<Response> { 
    url = "http://" + url; 
    this.setDefaultHeaders(parameters, url); 
    return this.http.get(url, { headers: this.headers }).toPromise(); 
}; 
private setDefaultHeaders(data: any, url: string): void { 
    this.headers = new Headers(); 
    this.headers.append("Content-Type", "application/json"); 
    this.headers.append("Authorization", "Basic " + data); 
    this.headers.append("Access-Control-Allow-Origin", url); 
} 
} 

兔子服務已啓動,其6個默認插件,你用命令

啓用

至於URL和參數,在函數接收到的URL爲localhost:15672/API /概述,參數爲來賓:來賓

回答

0

可以在this code看到CORS標頭返回時該請求包含Origin標題(match_origin函數)。

您使用Angular創建的請求必須包含Origin標題。

+0

它確實包含,我的應用程序的標題定義如下: this.headers = new Headers(); this.headers.append(「Access-Control-Allow-Origin」,url); this.headers.append(「Content-Type」,「application/json」); this.headers.append(「Authorization」,「Basic」+ data); –

+0

標題必須命名爲「Origin」 - https://github.com/rabbitmq/rabbitmq-management/blob/master/src/rabbit_mgmt_cors.erl#L72-L93 如果您可以提供重現此問題的代碼,修復它會變得更快。我只能猜測你做錯了什麼。 –

+0

Thanks =),剛剛使用我當前使用的代碼編輯我的原始帖子。 –