I have two domains. I\'m trying to access a JSON object from one domain through a page on another. I\'ve read everything I could find regarding this issue, and still can\'t
Some examples available here may illustrate further how access control can be combined with CORS. Specifically the credentialed GET example. Access control requires that the request set the withCredentials
flag to true
on the XMLHttpRequest
, and for the server handling the OPTIONS
method to do two things:
Access-Control-Allow-Credentials: true
*
in the Access-Control-Allow-Origin
header. This has to be set to the origin exactly according to the MDN docs on HTTP access control (CORS).Essentially, the thing processing the OPTIONS request needs to send back appropriate response headers so you can make that credentialed request.
In your question you stated that the service you are interacting with is returning Access-Control-Allow-Origin: *
, which is not compatible with a credentialed cross-domain request. This needs to return the origin specifically.
The aforementioned MDN Http Access Control (CORS) documentation also links to the Server-Side Access Control documentation outlining how a server would potentially respond to various cross domain requests - including handling a cross domain credentialed POST request that requires you to send back the correct headers in response to the OPTIONS method. You can find that example here.
Welp, now that I have enough rep a while later, I might as well answer this question and accept it.
When you attempt to send a GET
json request to a server with headers, the browser first sends an OPTION
request to make sure that you can access it. Unfortunately, this OPTION
request cannot carry with it any authentication. This means that if you want to send a GET
with auth, the server must allow an OPTION without auth. Once I did this, things started working.
Why don't you try typing the URL you are fetching the JSON from into your browser and seeing what happens. It sounds like you literally just need to authenticate into this other website to access it.
If your site needs to work in other browsers like IE, you WILL need JSONP, by the way. The security won't allow the cross site request to work. The headers won't change that. I believe you will also need to add a security policy in your headers.