为什么这个 CORS 请求 google 驱动器 Sheet 在 Firefox 中失败? (在 Chrome 工作)
Why does this CORS request to a google Drive Sheet fail in Firefox ? (works in Chrome)
我正在尝试使用 jquery ajax.
从 javascript 中的客户端请求 google sheet
以下代码在 Chrome 中有效,但在 Firefox 中失败。
问题:如何让它在 Firefox 中运行?
如果是服务器配置问题,这是否意味着无法从 Firefox 客户端 link 到 google 驱动文档?
代码如下:
var url = 'http://docs.google.com/spreadsheets/export?id=1-on_GfmvaEcOk7HcWfKb8B6KFRv166RkLN2YmDEtDn4&exportFormat=csv';
$.ajax({
url : url,
type : 'GET',
dataType : 'text',
success : function(res, status){
console.log('status : ' + status);
console.log(res);
},
error : function(res, status, error){
console.log('status : ' + status);
console.log(res);
console.log(error);
}
});
在 Chrome 中,我收到 307 响应,然后收到包含所需数据的 200 响应。
在 Firefox 中,我只收到 200 响应,但错误消息类似于 "Access-Control-Allow-Origin header missing, Same Origin Policy does not allow to fetch this resource".
问题是 docs.google.com
没有在重定向上设置 CORS headers。 Chrome 没有强制执行规范,因此没有遵循规范,因此存在各种安全漏洞。
docs.google.com
在 Chrome 的 HSTS 预加载列表中。对 http://docs.google.com
的请求被透明地重写为 https://docs.google.com
,因此不会发生重定向。
我假设如果 Firefox 提取 HSTS 预加载列表的更新副本,这将自行解决。正如安妮所说,只需将 link 直接更改为 https
即可解决您的用例。
我通过稍微不同地配置 Google 驱动器并使用 JSONP :
找到了解决方法
1) 在 Google 驱动器中,在网络上发布文档并将共享选项设置为 Public
2) 以 JSON 格式导出您的数据,JSON 类型 link,它看起来像:“http://spreadsheets.google.com/feeds/list/YOUR_FILE_ID/od6/public/values?alt=json&callback=myCallback”。您需要附加 &callback=myCallback 才能使用 JSONP。您可以使用 jQuery 拨打 JSONP 电话。
3) 要使用数据,需要定义url中指定的回调函数,本例为"myCallback"
我在另一个答案中提到了类似的过程,但我认为在这里提及它也很有用,因为它直接关系到我面临的问题。
@EnricoFerreguti 你应该用你的文件 ID 替换 YOUR_FILE_ID。示例:https://spreadsheets.google.com/feeds/list/1-on_GfmvaEcOk7HcWfKb8B6KFRv166RkLN2YmDEtDn4/od6/public/values?alt=json from the http://misterfresh.github.io/react-drive-cms/ 网站 .
我正在尝试使用 jquery ajax.
从 javascript 中的客户端请求 google sheet以下代码在 Chrome 中有效,但在 Firefox 中失败。
问题:如何让它在 Firefox 中运行?
如果是服务器配置问题,这是否意味着无法从 Firefox 客户端 link 到 google 驱动文档?
代码如下:
var url = 'http://docs.google.com/spreadsheets/export?id=1-on_GfmvaEcOk7HcWfKb8B6KFRv166RkLN2YmDEtDn4&exportFormat=csv';
$.ajax({
url : url,
type : 'GET',
dataType : 'text',
success : function(res, status){
console.log('status : ' + status);
console.log(res);
},
error : function(res, status, error){
console.log('status : ' + status);
console.log(res);
console.log(error);
}
});
在 Chrome 中,我收到 307 响应,然后收到包含所需数据的 200 响应。 在 Firefox 中,我只收到 200 响应,但错误消息类似于 "Access-Control-Allow-Origin header missing, Same Origin Policy does not allow to fetch this resource".
问题是 docs.google.com
没有在重定向上设置 CORS headers。 Chrome 没有强制执行规范,因此没有遵循规范,因此存在各种安全漏洞。
docs.google.com
在 Chrome 的 HSTS 预加载列表中。对 http://docs.google.com
的请求被透明地重写为 https://docs.google.com
,因此不会发生重定向。
我假设如果 Firefox 提取 HSTS 预加载列表的更新副本,这将自行解决。正如安妮所说,只需将 link 直接更改为 https
即可解决您的用例。
我通过稍微不同地配置 Google 驱动器并使用 JSONP :
找到了解决方法1) 在 Google 驱动器中,在网络上发布文档并将共享选项设置为 Public
2) 以 JSON 格式导出您的数据,JSON 类型 link,它看起来像:“http://spreadsheets.google.com/feeds/list/YOUR_FILE_ID/od6/public/values?alt=json&callback=myCallback”。您需要附加 &callback=myCallback 才能使用 JSONP。您可以使用 jQuery 拨打 JSONP 电话。
3) 要使用数据,需要定义url中指定的回调函数,本例为"myCallback"
我在另一个答案中提到了类似的过程,但我认为在这里提及它也很有用,因为它直接关系到我面临的问题。
@EnricoFerreguti 你应该用你的文件 ID 替换 YOUR_FILE_ID。示例:https://spreadsheets.google.com/feeds/list/1-on_GfmvaEcOk7HcWfKb8B6KFRv166RkLN2YmDEtDn4/od6/public/values?alt=json from the http://misterfresh.github.io/react-drive-cms/ 网站 .