Angularjs Ajax获取发送标头

时间:2015-06-15 21:07:05

标签: javascript ajax angularjs http

我是angularjs的新手,我正在为我的http请求使用服务。 其余的api之一我需要在标题中发送键值对。 用户名:foo 密码:bar

我如何使用我的服务中的http请求格式。 (我知道我需要在函数中传递参数,我不知道如何处理它以及对象格式)

.service(' UserService',[' $ http',' $ rootScope',function($ http,$ rootScope){

less<int>

}

...

//在控制器中

    this.CheckIfUserExists = function () {
        return $http.get($rootScope.endPoint + '/user/email_token');
    };

1 个答案:

答案 0 :(得分:0)

doc的示例 你需要知道什么样的auth。你可以使用post。例如。

.service('UserService',['$ http','$ rootScope',function($ http,$ rootScope){     var req = {      方法:'POST',      网址:'http://example.com',      标题:{        'Content-Type':未定义      },      数据:{test:'test'}     }

$http(req).success(function(){...}).error(function(){...});

在你的情况下:

.service('UserService', ['$http', '$rootScope', function ($http, $rootScope) {   
    this.CheckIfUserExists = function () {
        var req = {
            method: 'POST',
            url: 'http://example.com'
            data: { 'username': 'foo', 'password': 'bar' }
        };
        return $http(req);
    } 
}
相关问题