I am working on a Chrome extension, which is essentially a simple Google user form that will be hosted in an answer spreadsheet. I got the following function to successfully send and populate data only once, but never again:
function postFormToGoogle() { var timeOne = $("#time1hour").val(); var timeTwo = $('#time2hour').val(); var timeThree = $('#time3hour').val(); $.ajax({ url: "https://docs.google.com/forms/d/FORMKEY/formResponse", beforeSend: function (xhr) { xhr.setRequestHeader('Access-Control-Allow-Origin', 'chrome-extension://EXTENSION_ID'); xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, PUT'); }, data: { "entry_856586387": timeOne, "entry_244812041": timeTwo, "entry_2138937452": timeThree }, type: "POST", dataType: "xml", xhrFields: { withCredentials: true }, statusCode: { 0: function () { document.getElementById("message").innerHTML = "Your form has been submitted!"; window.location.replace("ThankYou.html"); }, 200: function () { document.getElementById("message").innerHTML = "Your form has been submitted!"; console.log("Success"); window.location.replace("ThankYou.html"); } } }); }
I had to include cors request headers because I received a No 'Access-Control-Allow-Origin' warning that blocked my request.
This extension, I also added the following permissions for the manifest.json file:
"permissions": [ "http://docs.google.com", "https://docs.google.com", "https://*.google.com", ]
At this point, I'm not sure what exactly prevents the publication of data. Possible indicators may be that when I submit the form, I get a warning “Preliminary headers” and the server reacts too long, as indicated by the timeout (TTFB).
Where am I mistaken in the code? (For some reason, this worked once.) Any alternative solutions for publishing a custom form in spreadsheets?