Angularjs Ajax 获取发送标头

Angularjs Ajax get sending headers

本文关键字:Ajax 获取 Angularjs      更新时间:2023-09-26

我是angularjs的新手,我正在使用服务来处理我的http请求。我需要在标头中发送键值对的其余 API 之一。用户名: 符密码:酒吧

如何使用服务中的 HTTP 请求格式执行此操作。(我知道我需要在函数中传递一个参数,我不知道如何去做以及什么对象格式)

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

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

}

控制器中

UserService.CheckIfUserExist()
      .success(function (data) {
        console.log(data);
        //handler
          }).
         error(function(error) {
        //handler
          });

文档中的示例您需要知道哪种身份验证,例如,您可以使用 POST。

.service('UserService', ['$http', '$rootScope', function ($http, $rootScope) { var req = { 方法:"开机自检", 网址: "http://example.com", 标头:{ "内容类型":未定义 }, 数据: { 测试: '测试' } }

$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);
    } 
}