A
A
Alexander Yelagin2016-05-10 23:27:28
Angular
Alexander Yelagin, 2016-05-10 23:27:28

Why doesn't Angularjs send CSRF-Token in request headers in Firefox browser?

Hello.
I don't understand why Angularjs (1.3.8) does not send a parameter in the header (CSRF-Token) when sending post requests.
The parameter itself is on the page, it works without problems in the Chrome browser, this parameter is sent, but for some reason it is not in Mazil.

<meta name="csrf-param" content="_csrf">
    <meta name="csrf-token" content="dDJERE4wN2JZUDEeGgJ7URV0cyV6U0RPFlk1CjlXURA2CiIhJwlGBg==">

Although in the config I specify the settings for sending this parameter. Tell me what I'm doing wrong?
var app = angular.module('LemmaApp',['kendo.directives','ngFileUpload','ngSanitize'], function($httpProvider) {
    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

    /**
     * The workhorse; converts an object to x-www-form-urlencoded serialization.
     * @param {Object} obj
     * @return {String}
     */
    var param = function(obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

        for(name in obj) {
            value = obj[name];

            if(value instanceof Array) {
                for(i=0; i<value.length; ++i) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value instanceof Object) {
                for(subName in value) {
                    subValue = value[subName];
                    fullSubName = name + '[' + subName + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }

        return query.length ? query.substr(0, query.length - 1) : query;
    };

    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
});

angular.module('LemmaApp').config(appconfig);
appconfig.$inject = ['$httpProvider'];
function appconfig($httpProvider){
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr('content');
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Yelagin, 2016-05-11
@juniorspecialist

Forcibly added headers with "X-CSRF-Token" to each request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question