AngularJS - 使用身份验证调用Web服务

时间:2015-04-02 07:51:04

标签: angularjs web-services http

我正在使用 -

进行网络服务调用
$http.get(url)

注意 - 1)我正在使用查询字符串动态创建URL。 2)当我在地址栏中输入网址时,它会要求输入登录凭据。

问题 - 1)如何在网址中添加登录凭据,而我是动态创建的?我知道这不是一个好主意,有没有更好的方法来处理这种情况?

2)我从$ http调用中获取数据。是因为我之前登录过一次并且同一会话正在继续吗?

1 个答案:

答案 0 :(得分:0)

是标准HTTP凭据吗?如果是这样,您可以使用配置标志

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

您的服务:

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

    var service = {};

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

    return serivce;

}])

取自: https://docs.angularjs.org/api/ng/service/ $ HTTP