我有一个使用.appcache清单的应用程序.一切正常,资源得到缓存:
- CACHE MANIFEST
- CACHE:
- css/images/ajax-loader.gif
- [...]
- NETWORK:
- http://docs.google.com/*
- SETTINGS:
- prefer-online
现在,当我从http://docs.google.com请求CSV资源时,如下所示:
- $.get(url,function (data) {
- // do something with the data
- }).fail(function () {
- alert("There was an error in loading current data from the server.");
- });
即使我在线(在Chrome和FF上),请求也会失败.在使用Appcache之前,一切正常.
在拥有Appcache清单时请求外部资源的正确方法是什么?
解决方法
正如我在评论中提到的,问题不是(仅)jQuery,而是CORS. Google Docs缺乏对它的支持(“发布到网络”意味着下载,而不是从不同的URL请求资源,即来源.显然谷歌不想添加标题集Access-Control-Allow-Origin“* ”.
所以我所做的就是遵循这个有用的指南:https://webapps.stackexchange.com/questions/11864/how-can-i-retrieve-records-from-a-google-spreadsheet-in-json-format和https://developers.google.com/gdata/samples/spreadsheet_sample(URL现在不是CSV而是JSONP,它也改为//spreadsheets.google.com/Feeds/list / […] / [… ] / public / values?alt = json-in-script和jQuery在调用时自动添加callback = xxx:
这不是很好或干净的任何方式(Atom Feed而不是CSV或JSON只是为了解析它?严重吗?),但它适用于我.