Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request header field Pragma is not allowed by Access-Control-Allow-Headers in preflight response

I am getting this error when I was trying to keep 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 am getting error in chrome as follows:

Request header field Pragma is not allowed by Access-Control-Allow-Headers in preflight response.

But when I remove the following code, its working 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 any one tell me what could be the problem?

like image 977
user1268130 Avatar asked Mar 25 '16 11:03

user1268130


People also ask

How do you fix ensure CORS response header values are valid?

The CORS request requires that the server permit the use of credentials, but the server's Access-Control-Allow-Credentials header's value isn't set to true to enable their use. To fix this problem on the client side, revise the code to not request the use of credentials.

How do I fix a blocked CORS policy?

Use a Chrome extension to add Access-Control-Allow-Origin header into every response. To find one of them, just head over to Chrome Webstore and type in "CORS", dozens will show up in the search result. Or you can install CORS Helper, CORS Unblock or dyna CORS right away.

Can HTTP headers alone restrict or allow access to resources from specified origins?

HTTP headers alone cannot restrict or allow access to resources from specified origins.


Video Answer


1 Answers

If you can configure in server side to accept those headers, then it's fine. Else, you should remove those header which is set in $httpProvider.defaulsts. Check 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)
});
like image 58
firesh Avatar answered Oct 25 '22 01:10

firesh