Skip to content Skip to sidebar Skip to footer

How Do I Execute An Ajax Post With Request Header In Angular

I am having a working code in jquery, where I post data from form with a token using request.setRequestHeader('X-CSRF-Token', $.cookie('token'));. I know how to get the form data i

Solution 1:

You can use $http service to do that. You'd better create you own service, Demo code:

var demoApp = angular.module("DemoApp");
demoApp.controller("yourcontroller", function($scope, yourService){
    yourService.postData("http://your/url", {"X-CSRF-Token": $scope.token})
        .success(function(data){
            //handle success response here
        }).error(function(error){
            //handle error response here 
        });                                  
});
demoApp.service("yourService", function($http){
    this.postData = function(url, _headers){
        return$http({
                  method: "POST", 
                  url: url,
                  headers: _headers
               });
     };
});

Here is angular $http document.

Post a Comment for "How Do I Execute An Ajax Post With Request Header In Angular"