Pragma request header field is invalid with Access-Control-Allow-Headers in preflight response

I get this error when I try to save the following code to disable cache for ajax

angularApp.config(['appConfig', '$httpProvider', function (appConfig, $httpProvider) {

if (!$httpProvider.defaults.headers.get) {
    $httpProvider.defaults.headers.get = {};
}

//disable IE ajax request caching
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';

}]);

I get an error in chrome as follows:

The Pragma request header field is not allowed by the Access-Control-Allow-Headers headers in the preflight response.

But when I delete the following code, it works fine.

$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';

can anyone tell me what could be the problem?

+4
source share
1 answer

If you can configure on the server side to accept these headers, then this is normal. In addition, you must remove the headers that are set to $ httpProvider.defaulsts. Check out the code below:

var data = {}
var httpCoonfig = {
    headers: {'Pragma': undefined, 'Cache-Control': undefined, 'X-Requested-With': undefined, 'If-Modified-Since': undefined}
};
$http.post('https://www.google.com/', data, httpCoonfig).then(function(response){
// console.log(response)
}, function(response){
     console.log(response)
});
+1

All Articles