Cross-origin requests on Google CDN
-
07-07-2021 - |
سؤال
I am trying to load a file with a .json extension from the Google CDN:
//ajax.googleapis.com/ajax/libs/dojo/1.8/dojox/geo/charting/resources/data/USStates.json
Standard xhr requests fail because of the cross-origin policy. Does the Google CDN support any kind of cross-origin request, like JSONP or CORS? Could you show me an example of how to grab the above file?
The above file is part of Dojo, but I am fine with using any other library or plain JavaScript. The only constraint is to grab the file from the Google CDN.
المحلول
Based on the (lack of) information I got, the Google CDN doesn't support any kind of cross-domain access like JSONP or CORS. It means that the only way to consume the CDN .json files is through a server proxy.
For client side JavaScript I'll have to store a local copy of the file.
نصائح أخرى
I know it been a long time since this question was asked but now google storage lets us change CORS configurations.
I was having the same issue regarding CORS and followed these steps to resolve it.
- Create JSON file with CORS configurations cors-config.json
[
{
"origin": ["*"],
"method": ["GET"],
"maxAgeSeconds": 3000,
"responseHeader": ["Authorization", "Content-Range", "Accept", "Content-Type", "Origin", "Range"]
}
]
2. Upload this file in bucket
gsutil cp cors-config.json gs://[bucket_name]
3. Now update bucket cors configuration
gsutil cors set cors-config.json gs://[bucket_name]
Your CORS configurations are updated. Now you can see newly updated CORS configs on the bucket by running this command.
gsutil cors get gs://[bucket_name]/
Google document about CORS config
It doesn't seem to be the case anymore:
$.ajax('//ajax.googleapis.com/ajax/libs/dojo/1.8/dojox/geo/charting/resources/data/USStates.json').
then(function(data) { console.log(data) })
// {"layerExtent": …}
At time of this writing, @Yogesh Patil's answer did solve the problem.
I would like to point out that, I am using Windows Google Cloud SDK.
You don't need to upload the json file to your bucket. But you need to copy the json file into your local Google Cloud SDK directory. Then you can run the command :
gsutil cors set cors-config.json gs://[bucket_name]