1

how i do this code in Angular JS

$.ajaxSetup({
   complete: function (jqXHR) {
      var new_token = jqXHR.getResponseHeader('X-CSRF-Token');
      $('meta[name="_token"]').attr('content', new_token);
   }
});

i want set new token when i post with angular $http

6
  • Try this in configuration of your module: $httpProvider.defaults.headers.common["X-CSRF-TOKEN"] = "YOUR_TOKEN; Commented Aug 1, 2018 at 9:50
  • i did it , not working Commented Aug 1, 2018 at 9:55
  • my code var AH_App = angular.module('AH_App', [ 'AH_App.services', 'AH_App.controller', ]).config(function ($httpProvider) { $httpProvider.interceptors.push('interceptors'); $httpProvider.defaults.headers.common['Accept'] = 'application/json, text/javascript'; $httpProvider.defaults.headers.common['Content-Type'] = 'application/json; charset=utf-8'; $httpProvider.defaults.headers.common['X-CSRF-TOKEN'] = $('meta[name="csrf-token"]').attr('content'); }); Commented Aug 1, 2018 at 9:56
  • i need do like this $.ajaxSetup({ complete: function (jqXHR) { var new_token = jqXHR.getResponseHeader('X-CSRF-Token'); $('meta[name="csrf-token"]').attr('content', new_token); } }); Commented Aug 1, 2018 at 10:01
  • I think that @Madhan Varadhodiyil solution is for you. Commented Aug 1, 2018 at 10:21

2 Answers 2

1

OK, this should working:

Factory Definition:

myApp.factory('tokenInterceptorService', ['$q',
    function CsrfTokenInterceptorService($q) {

        var CSRF_TOKEN_HEADER = 'X-CSRF-TOKEN';

        var token;


        var service = {
            response: onSuccess,
            responseError: onFailure,
            request: onRequest,
        };

        return service;

        // Private functions.
        function onFailure(response) {
            if (response.status === 403) {
                //DO SOMETHING
            }

            return $q.reject(response);
        }

        function onRequest(config) {
            //SET TOKEN TO SEND
            config.headers[CSRF_TOKEN_HEADER] = token;


            return config;
        }

        function onSuccess(response) {
            //SET HERE NEW TOKEN FROM REQUEST RESPONSE
            var newToken = response.headers(CSRF_TOKEN_HEADER);


            if (newToken) {
                token = newToken;
            }

            return response;
        }
    }
]);

Configuration Definition:

.config(function($httpProvider) {
    $httpProvider.interceptors.push('tokenInterceptorService');
});
Sign up to request clarification or add additional context in comments.

4 Comments

can you write $http an example code for do method POST
$http.post('/someUrl', {key: value}).then(function(data){//handle here data}); So when you call: in onRequest Method you will set at every call your header token in onSuccess Method you will read response header (CSRF_TOKEN_HEADER) and update your token
can you edit your solutions with my this code factory("interceptors", [function () { return { 'request': function (request) { if (request.beforeSend) request.beforeSend(request); return request; }, 'response': function (response) { if (response.config.success) response.config.success(response); return response; } }; }]);
I'm not sure what you want, can you explain please with some details?
0

You can use interceptors to push the token for http requests

//Assuming you've created an module,

app.config([ '$httpProvider',   function($httpProvider) {
 $httpProvider.interceptors.push('CSRFInterceptor');
}]);
app.service('CSRFInterceptor', [function() {
this.request = function(config) {
    config.headers['X-CSRF-Token']= getCSRFToken();
    return config;
};
}]);

2 Comments

what about function getCSRFToken()
If you're using cookie based CSRF token mechanism you can get it from cookie

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.