为什么当我使用Appcache清单请求外部资源时jQuery会抛出一个错误? [英] Why does jQuery throw an error when I request external resources using an Appcache Manifest?

查看:184
本文介绍了为什么当我使用Appcache清单请求外部资源时jQuery会抛出一个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用.appcache清单的应用程式。一切正常工作,资源缓存:

I have an application that uses a .appcache manifest. Everything works as expected, resources get cached:

CACHE MANIFEST

CACHE:
css/images/ajax-loader.gif
[...]

NETWORK:
http://docs.google.com/*

SETTINGS:
prefer-online

现在,当我请求 http://docs.google.com 如下:

$.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之前,一切工作正常。

the request fails even if I'm acutally online (on both Chrome and FF). Everything worked fine before I used the Appcache.

在使用Appcache清单时请求外部资源的正确方法是什么?

What is the correct way of requesting external resources while having an Appcache manifest?

推荐答案

正如我在我的评论中提到的,问题不是(只)jQuery,而且还有CORS。 Google文档缺乏对其的支持(发布到网络是指下载,而不是从其他网址(即来源)请求资源。显然Google不想添加标题集访问控制-Allow-Origin*

As I mentioned in my comment, the problem was not (only) jQuery, but also CORS resp. Google Docs' lack of support of it ("Publish to web" means for download, not for requesting the resource from a different URL, i.e. origin. Apparently Google does not want to add a Header set Access-Control-Allow-Origin "*".

所以我做的是按照这个有帮助的指南:http://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 (网址现在不是CSV,而是JSONP,也改为 // spreadsheets.google.com / feeds / list /[...]/[.../ public / values?alt = json-in-script 和jQuery自动添加 callback = xxx 时调用:

So what I did instead was follow this helpful guide: http://webapps.stackexchange.com/questions/11864/how-can-i-retrieve-records-from-a-google-spreadsheet-in-json-format and https://developers.google.com/gdata/samples/spreadsheet_sample (The URL is now not CSV but JSONP, it also changed to //spreadsheets.google.com/feeds/list/[...]/[...]/public/values?alt=json-in-script and jQuery automatically adds the callback=xxx when called with:

$.ajax({
  url : url,
  type : 'GET',
  dataType : 'jsonp',
  success : function (data) {
    for(var i in data.feed.entry) {
      console.log(entry['gsx$' + 'actualColumnHeader'].$t);
    }
  },
  error : function () {
    alert('Failed');
  }
});

这不是很好或干净的任何方式(Atom Feed,而不是CSV或JSON只是为了解析它回来?严重?),但它适用于我。

This is not nice or clean by any means (Atom Feed instead of CSV or JSON just to parse it back? Seriously?), but it works for me.

这篇关于为什么当我使用Appcache清单请求外部资源时jQuery会抛出一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆