admin管理员组文章数量:1425023
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 badges4 Answers
Reset to default 3Based 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 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": …}
本文标签: javascriptCrossorigin requests on Google CDNStack Overflow
版权声明:本文标题:javascript - Cross-origin requests on Google CDN - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745407199a2657305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论