JQuery .ajax POST到Spreadsheets API?

时间:2012-01-20 02:30:45

标签: jquery google-spreadsheet-api

我正在尝试通过JavaScript / JQuery在Google电子表格中添加新行。我不知道该怎么做,但这是我的尝试:

        var myJSONObject = {"entry": [
         "gsx:name": name,
         "gsx:email": email,
         "gsx:phone": phone,
         "gsx:guests": guests
        ]}

        var json_uri = "https://spreadsheets.google.com/feeds/list/" + KEY + "/" + id + "/" + SECURE + "/values?alt=json"

        $.ajax({
         type: "POST",
         url: json_uri,
         //contentType: 'application/json',
         data: myJSONobject
         dataType: 'json',
         success: function(data){
            alert("row added!");
         },
         error: function(){
            alert("error");
         }
    });

但我一直收到错误:(

1 个答案:

答案 0 :(得分:1)

我不确定如何使用jQuery ajax api,但您可以尝试构建一个xml条目,如下所示设置参数。

也许其他人可以用jQuery api来展示如何做到这一点?

gdocs.constructSpreadAtomXml_ = function(name, email, phone, guests) {    
    var atom = ["<?xml version='1.0' encoding='UTF-8'?>",
              '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">',
              '<gsx:name>',name,'</gsx:title>',
              '<gsx:email>',email,'</gsx:url>',
              '<gsx:phone>',phone,'</gsx:summary>',
              '<gsx:guests>',guests,'</gsx:tags>',
              '</entry>'].join('');
    return atom;
};

...

var params = {
    'method': 'POST',
    'headers': {
      'GData-Version': '3.0',
      'Content-Type': 'application/atom+xml'
    },

    'body': gdocs.constructSpreadAtomXml_(name, email, phone, guests)
};

  var worksheetId = 'od6'; //The first worksheet.

  var url = 'https://spreadsheets.google.com/feeds'+'/list/'+docId+'/'+worksheetId+'/private/full';

  //sends the params to the background page to get delivered to gDocs
  oauth.sendSignedRequest(url, handleSuccess, params);