为什么在 Firefox 中对 google Drive Sheet 的 CORS 请求失败?(适用于 Chrome)

Mis*_*esh 5 ajax firefox xmlhttprequest cors google-drive-api

我正在尝试使用 jquery ajax 在 javascript 中向客户端请求 google 工作表。

以下代码在 Chrome 中有效,但在 Firefox 中失败。

问题:如何让它在 Firefox 中工作?

如果这是服务器配置问题,那么这是否意味着无法从 Firefox 客户端链接到 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);
    }
});
Run Code Online (Sandbox Code Playgroud)

在 Chrome 中,我收到 307 响应,然后收到包含所需数据的 200 响应。在 Firefox 中,我只收到 200 响应,但出现类似“Access-Control-Allow-Origin header丢失,同源策略不允许获取此资源”之类的错误消息。

Mis*_*esh 0

我找到了一种解决方法,通过稍微不同地配置 Google Drive 并使用 JSONP :

1) 在 Google Drive 中,在网络上发布文档并将共享选项设置为公开

2) 使用 JSON 类型链接以 JSON 格式导出数据,它将如下所示:“ 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 您应该将 YOUR_FILE_ID 替换为您的文件 ID。示例:https://spreadsheets.google.com/feeds/list/1-on_GfmvaEcOk7HcWfKb8B6KFRv166RkLN2YmDEtDn4/od6/public/values ?alt=json,来自http://misterfresh.github.io/react-drive-cms/网站。