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

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

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": …}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745407200a4626369.html

相关推荐

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

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

    12小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信