2015-04-02 59 views
0

我正在使用Web服務調用web服務 -AngularJS - 調用帶有認證

$http.get(url) 

注 - 1)我動態創建與查詢字符串的URL。 2)當我在地址欄中輸入網址時,它會要求登錄憑證。

問題 - 1)如何在url中添加登錄憑證,而我正在動態創建它?我知道這不是一個好主意,有沒有更好的方法來處理這種情況?

2)我從$ http調用中獲取數據。是因爲我之前登錄過一次,同一會話還在繼續?

回答

0

它是標準的HTTP憑據嗎?如果是這樣,你可以使用配置標誌

withCredentials - {boolean} - whether to set the withCredentials flag on the XHR object. See requests with credentials for more information. 

您的服務: https://docs.angularjs.org/api/ng/service/ $ HTTP

app.service('myService', ['$http', function($http){ 

    var service = {}; 

    service.getUrlWithCredentials = function(url){ 
     return $http(url, 
     { 
      withCredentials : true 
     } 
    } 

    return serivce; 

}]) 

來自