最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cross-origin requests on Google CDN - Stack Overflow

programmeradmin2浏览0评论

I am trying to load a file with a .json extension from the Google CDN:

//ajax.googleapis/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.

I am trying to load a file with a .json extension from the Google CDN:

//ajax.googleapis./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.

Share Improve this question asked Oct 16, 2012 at 20:48 ChristopheChristophe 28.2k29 gold badges103 silver badges144 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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.

  1. 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 mand.

gsutil cors get gs://[bucket_name]/

Google document about CORS config

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 mand :

gsutil cors set cors-config.json gs://[bucket_name]

It doesn't seem to be the case anymore:

$.ajax('//ajax.googleapis./ajax/libs/dojo/1.8/dojox/geo/charting/resources/data/USStates.json').
  then(function(data) { console.log(data) })

// {"layerExtent": …}
发布评论

评论列表(0)

  1. 暂无评论