Nginx services fails for cross-domain requests if the service returns error

view story

http://serverfault.com – I am making a cross domain request in my web app. I have set the CORS headers on Nginx. Everything is working fine except when the service returns an error like 404, 400, 500 etc, instead of receiving the error code, the service is failing with an error saying that the Origin *********** is not allowed by Access-Control-Allow-Origin. Any ideas why this might be happening? This is what the nginx setting looks like; location /api { add_header Access-Control-Allow-Origin *; } (HowTos)