简体   繁体   English

AngularJS $ resource请求,带有带有用户名和密码的凭据

[英]AngularJS $resource request, withCredentials with username and password

I'm trying to do a small application that should fetch json data from a homepage with basic http authentication. 我正在尝试做一个小应用程序,该应用程序应使用基本的HTTP身份验证从主页获取json数据。

This is my service: 这是我的服务:

app.factory('MySrvs', ['$resource', function ($resource) {

    return $resource("http://address/to/json/document");

}]);

This is my controller: 这是我的控制器:

app.controller('MyCtrl', ['$scope', 'MySrcvs', 'Base64',
function($scope, MySrvs, Base64) 
var stuff =  MySrvs.query(function(data){
   console.log(data)
   $scope.mylist = data;
});

This works good for my mirrored page without auth. 这对于没有身份验证的镜像页面非常有用。

However, the production should go to a page that needs withCredentials:true with username and password to set up. 但是,生产应该转到需要withCredentials:true以及需要设置用户名和密码的页面。 Where should this be added? 应该在哪里添加? I have access to a Base64 encoder. 我可以访问Base64编码器。

Gratefull for any help. 非常感谢您的帮助。

The $resource configuration object is very similar to the $http configuration object (with a few changes). $ resource配置对象与$ http配置对象非常相似(有一些更改)。 The value of the object, the action, is the name of the method on the resource object 对象的值(动作)是资源对象上方法的名称

for example 例如

angular.module('myApp', ['ngResource'])
    .factory('UserService', [
        '$resource', function($resource) {
            return $resource('/api/users/:id', {
                id: '@'
            }, {
                query: {
                    method: 'PUT', // or something else 
                    headers:{'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='} 
                }
            });
        }]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM