javascript - 谷歌电子表格 - ajax 调用(获取和发布)

标签 javascript jquery google-docs

我需要做的是读取“公共(public)”谷歌电子表格的内容(公共(public)是指我点击“文件>发布到网络”保存了该工作表,因此无需登录即可访问它进入谷歌帐户),为什么不,也写一些东西进去。

google了一下,我发现我可以访问该工作表并使用类似的内容获取与工作表内容等效的 xml

https://spreadsheets.google.com/feeds/list/<sheetCode>/od6/public/values

如果我将该网址加载到浏览器中,效果会很好。但我需要找到一种“javascript-way”来获取和处理返回值,即xml(或json,但xml更好)。

我尝试使用ajax调用,但我认为协议(protocol)有些困惑..我无法正确获得服务器响应。

$.ajax({
    type: "GET",
    url: "https://spreadsheets.google.com/feeds/list/<sheetCode>/od6/public/values",
    success: function(data){alert("yeah");},
    error: function(){alert("fail..");},
    dataType:"xml",
});

我还尝试获取 json 而不是 xml,将“?alt=json”添加到 url 并更改数据类型,但我仍然遇到问题..

有什么想法/建议吗?

提前致谢,最诚挚的问候

最佳答案

您需要使用 JSONP 调用进行请求,并且需要指定回调方法。这可以在 jQuery 中使用以下方法完成:

var url = 'https://spreadsheets.google.com/feeds/list/<CODE>/od6/public/values?alt=json-in-script&callback=?';
jQuery.getJSON(url).success(function(data) {
    console.log(data); 
}).error(function(message) {
    console.error('error' + message); 
}).complete(function() {
    console.log('completed!'); 
});

google spreedsheets 的文档和示例.

关于javascript - 谷歌电子表格 - ajax 调用(获取和发布),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13478160/

相关文章:

google-maps - 如何在仅包含城市名称的谷歌地图 API 上获取城市的 Lat、Alt

javascript - 如何使用 jquery 添加 div 的副本?

JavaScript 查询在函数内无法正常工作,没有显示结果

jQuery iCheck 回调

google-apps-script - Google Apps 脚本 - 如何在多个容器中拥有一个脚本?

google-docs - Google Doc Query 在一张表中工作,但在另一张表中给出解析错误

javascript - jQuery slider PHP/HTML 问题

javascript - 从 Node + Express API 中提取使用 res.json 发送的信息

javascript - 将 div 的内容复制到剪贴板

javascript - 使用 API 通过 Nodejs 使用 Drive.files.copy 将 Word 文档转换为 Google 文档 在 Google Drive API v3 中进行转换