1

as the title says, I'd like to know if it's possible to send some sort of JSON to a google app script, using POST method. Once the JSON is sent, it will be handled by the google script.

Till now, I managed to send a post request using a standard html form with 3 inputs (I followed this guide: http://mashe.hawksey.info/2011/10/google-spreadsheets-as-a-database-insert-with-apps-script-form-postget-submit-method/). Now I'd like to move to an ajax call that send some json to the google script, but I don't know how..

using an ajax call like this:

$.ajax({
  type: "POST",
  url: url,
  data: "someSortOfData",
  success: function(data){alert("ok")},
  error: function(message){alert("fail")}
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

where "someSortOfData" is the actual data I'm sending (I tryed with json, simple text, html, etc). Any time I run the script, the call keeps entering the "error" function, and I don't know why.

As I told before, using a standard html form for submitting data works, so I presume the URL is fine. The problem is with the data I'm sending I think.. Maybe, server side, google script is not expecting anything different than text?

this is how the script (Server side) looks like

function doPost(e) {
  var ss = SpreadsheetApp.openById(ScriptProperties.getProperty('active'));
  var sheet = ss.getSheetByName("DATA");
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
  var nextRow = sheet.getLastRow(); // get next row
  var cell = sheet.getRange('a1');
  var col = 0;
  for (i in headers){
    val = e.parameter[headers[i]]; 
    cell.offset(nextRow, col).setValue(val);
    col++;
  }

  //return sent data
  var app = UiApp.createApplication();
  var panel = app.createVerticalPanel();
  for( p in e.parameters){
    panel.add(app.createLabel(p +" "+e.parameters[p]));
  }
  app.add(panel);
  return app;
}

I probably need to edit this code too, but first I was trying to send correctly json to the server..

What I'm doing wrong?

j0k
  • 22,600
  • 28
  • 79
  • 90
BeNdErR
  • 17,471
  • 21
  • 72
  • 103
  • I forgot to say that the google script writes data coming from the post request into a google spreadsheet. – BeNdErR Nov 22 '12 at 10:20
  • 1
    Where are you trying to make the AJAX call from? Is that a separate web app on top of Google Apps Script? Could you describe the high level separation you have here? – Arun Nagarajan Nov 26 '12 at 13:26
  • As I mentioned in the post I tried using ajax to submit the data without refresh but you run into cross domain/XHTTP security issues. The answer here http://stackoverflow.com/questions/10000020/ajax-post-to-google-spreadsheet highlights the example I use using a hidden iframe trick you might be able to modify – mhawksey Nov 27 '12 at 08:31

1 Answers1

-1

As noted in comments, it does sound potentially like a client-side cross-domain-scripting issue. Where the AJAX call is coming from is key - it seems GAS will generally give you a 200 even for errors in generating output, which should be "success".

You might change your "fail" message to be alert("fail:" + JSON.stringify(message)) to get a little more debug info, but in the case of cross-domain-scripting block, info may be sparse (e.g., "No Transport"). Perhaps mhawksey's fix is the ticket, or try using JSONP mode of JQuery AJAX fxnality (see docs, especicially dataType, or this answer).

Community
  • 1
  • 1
Brian Henry
  • 3,161
  • 1
  • 16
  • 17