我需要做的是读取“公共(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/