Error CORS header 'Access-Control-Allow-Origin' does not match 'example.com'

I am facing a serious problem in MEAN STACK. When i tried to get data from directly running url from browser then its return data but when i run the same url from ajax in Restangular its return error ie.  

"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/companies. (Reason: CORS header 'Access-Control-Allow-Origin' does not match 'example.com')."

After googling and searching finally i find out the sollution of it :

Solution : I added a code on my server file 

var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', 'http://yourUrl.com');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
}


app.use(allowCrossDomain); 



Note : Don't forgot to change yourUrl.com with your own.

Chears :)
Happy Coding ..  

Post a Comment

Previous Post Next Post