I have two domains. I am trying to access a JSON object from one domain through a page to another. I read everything I could find on this issue, and still can not understand it.
The JSON domain has the following settings:
Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "GET, OPTIONS" Header set Access-Control-Allow-Headers "origin, authorization, accept"
From my other domain, I call the following:
$.ajax({ type:'get', beforeSend: function(xhr) { var auth = // authentication; xhr.setRequestHeader("Authorization", "Basic " + auth); } url:myUrl, dataType:'json', error: function(xhr, textStatus, errorThrown) { console.log(textStatus, errorThrown); } })
I know that "auth" is initialized correctly (registered and verified). However, this does not work. In the Firefox Console, I get the request URL: ...
Request Method: OPTIONS Status Code: HTTP/1.1 401 Authorization Required
If I get rid of the beforeSend:... part beforeSend:... , I see the following
Request Method: GET Status Code: HTTP/1.1 401 Authorization Required
However, a JSON serving domain can also serve JSONP. I do not want to use this, mainly because the application will constantly work in a dedicated browser, and I am worried about this problem . More importantly, I would really like to know what is really wrong with what I do. I know that for practical purposes there are various ways to overcome JSONP memory leak (for example, without using jQuery).
Anyway, when I used JSONP, my code looked like this:
$.ajax({ url:newUrl, dataType:'jsonp', jsonp:'jsonp' }).done(function(d){console.log(d)})
It turns out the following
Request Method: GET Status Code: HTTP/1.1 200 OK
after he prompts me a warning window for the username and password.
Is there a fundamental difference in how jQuery handles JSONP requests, not JSON requests? And if so, how can I fix it?
Thanks.
Edit: this is what I found.
Basically, since I need authentication, the GET request sends an authorization header. However, this is not a "simple" header, so the browser sends a request before the flight (OPTIONS). However, this pre-validation request has no authentication, and therefore the server rejected it. The "solution" was to configure the server so that the OPTIONS request did not require authentication and report it with an HTTP status of 200.
Link: http://www.kinvey.com/blog/item/61-kinvey-adds-cross-origin-resource-sharing-cors
mail-archive [.com] / c-user@axis.apache.org /msg00790.html(not allowed to post more links)
Unfortunately, the “solution” only works with Firefox, not Chrome. Chrome just shows the request in red, but it does not give me more information about why it failed.
Edit 2: Fixed in Chrome. The server I was trying to get data on had a security certificate that was not trusted. Because of this, a preview request in Chrome. Solution superuser [.com] / questions / 27268 / how-do-i-disable-the-warning-chrome-give-if-a-security-certificate-is-not-trust (no more links allowed)