Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

angularjs - How to specify headers parameter for custom Angular $resource action

The following works fine, but I am thinking this modifies the $httpProvider globally, which isn't what I want.

angular.module('SessionService', ['ngResource'])
    .config(function($httpProvider){
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    })
    .factory('Login', function($resource){
        var resource = $resource('/adminui/login',{},{
            post:{
                method:"POST",
                isArray:false
            },
        });
        return resource;
    })
LoginCtrl = function($scope,Login) {
    $scope.login = function(){
        Login.post($.param({user:$scope.user.username,password:$scope.user.password}),$.noop,$.noop)
    }
}

Is there anyway to do this instead?

...
    .factory('Login', function($resource){
        var resource = $resource('/adminui/login',{},{
            post:{
                method:"POST",
                isArray:false,
                headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} // ignored
            },
        });
        return resource;
    })

The "headers" parameter seems to be ignored. the request is still

Content-Type:application/json;charset=UTF-8

Is my value for headers ok?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have confirmed that 1.1.3 does indeed support this. However, you need to make sure you also get the 1.1.3 version of the resource service. A quick test of:

angular.module('myApp', ['ngResource']).
  config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/', {templateUrl: 'partials/partial1.html',controller: 'MyController'});
    $routeProvider.otherwise({redirectTo: '/'});
  }])

  .controller("MyController", function( $scope, Bug) {
    Bug.post({test:"test"});
  })

  .factory('Bug', function($resource){
    var resource = $resource('/bug',{},{
        post:{
            method:"POST",
            isArray:false,
            headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} 
        },
    });
    return resource;
});

This will make a request with the headers set to (confirmed using Chrome):

Content-Type:application/x-www-form-urlencoded; charset=UTF-8

A quick note, I was unable to find a download of the angular-resource.js, so I had to go the the github website to download it. It is here.

For some giggles, I created a fiddle. Notice that there will be a failed POST call, but its headers are set correctly. Example Fiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...