QUESTION:
I am getting an error whenever I make an AJAX request to my subdomain.
The error tells me there is no 'Access-Control-Allow-Origin' header present on the requested resource.
Yet, I seem to have specified an 'Access-Control-Allow-Origin' where needed ?
What mistake have I made and how do I fix ti ?
ERROR:
Failed to load https://subdomain.example.com/endpoint: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.example.com' is therefore not allowed access. The response had HTTP status code 500.
CODE:
Client
$.ajax({
        type: "POST",
        url: "https://subdomain.example.com/endpoint",
        data: theData,
        headers: {  
            'Access-Control-Allow-Origin': 'https://example.com',
            'Access-Control-Allow-Credentials': true
        },
        timeout: 600000,
        async: true,
        xhrFields: {withCredentials: true},
        crossDomain: true
    }).done(function(response) {
Server
var whitelist = ['https://example.com'];
var corsOptions = {
    origin: function (origin, callback) {
        if (whitelist.indexOf(origin) !== -1) {
            callback(null, true)
        } else {
            callback(new Error('Not allowed by CORS'))
        }
    },
    credentials: true
}
router.options('/', cors(corsOptions), function(req,res,next){
    res.setHeader('Access-Control-Allow-Origin', 'https://example.com');
    res.status(200).end();
});
router.post("/", cors(corsOptions), function(req, res, next){
    res.setHeader('Access-Control-Allow-Origin', 'https://example.com');
    res.setHeader('Access-Control-Allow-Credentials', true);
 ...etc...
 
     
     
    