Getting the Content-Type with AngularJS or jQuery Getting the Content-Type with AngularJS or jQuery ajax ajax

Getting the Content-Type with AngularJS or jQuery


You can get all the headers with $http:

$http({method: 'GET', url: '/someUrl'}).  success(function(data, status, headers, config) {      var contentType = headers('Content-Type');      // use the content-type here  })


You can do this with jQuery:

$.ajax({    url:'/someUrl',    type: 'GET',    success:function(res, status, xhr){        var contentType = xhr.getResponseHeader('Content-Type');    }});


After more than two hours of searching, this is the best answer I found for angularJS 1.x:

$http({    method: method,    url: url,    data: data,    header: {'Content-Type': 'application/x-www-form-urlencoded'},    timeout: 30000})    .then(        //success function        function(response){            console.log(response.headers('content-type'));        },        //error function        function(response){        }    );

The main part is response.headers('content-type')

Note 1: This solution is applied to angular 1.x.

Note 2: This method is using then(success function,error function), which is much more better and reliable than the elder method (separate success and error)