How to make XMLHttpRequest cross-domain withCredentials, HTTP Authorization (CORS)?
I've written an article with a complete CORS setup.
I found several issues that can result in this problem:
- The
Access-Control-Allow-Origin
cannot be a wildcard if credentials are being used. It's easiest just to copy theOrigin
header of the request to this field. It's entirely unclear why the standard would disallow a wildcard. - Firefox caches the Access-Control results even if you clear the cache (perhaps for the session). Restarting forced it to do a new
OPTIONS
request. To aid in debugging I added the headerAccess-Control-Max-Age: 1
- The username/password of the
open
command is apparently not usable as the credentials. You must add anAuthorization
header yourself.xhr.setRequestHeader( 'Authorization', 'Basic ' + btoa( user + ':' + pass ) )
Overall the withCredentials
system is rather braindead. It's easier to simply write a server that accepts the authorization as part of the body of the request.