How to send Google Forms data via jQuery and Ajax to spreadsheets

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?

+5
source share
1 answer

So I did it ... http://jsfiddle.net/adutu/7towwv55/1/ You can see that you get a CORS error, but it works ... the data goes where it should be

  function postToGoogle() { var field3 = $('#feed').val(); $.ajax({ url: "https://docs.google.com/forms/d/[key]/formResponse", data: {"entry.347455363": field3}, type: "POST", dataType: "xml", statusCode: { 0: function() { //Success message }, 200: function() { //Success Message } } }); } 

More here

+8
source

Source: https://habr.com/ru/post/1216136/


All Articles