简体   繁体   English

如何通过jQuery和Ajax将Google表单数据发布到Spreadsheets

[英]How to Post Google Forms Data via jQuery and Ajax to Spreadsheets

I'm working on a Chrome extension that's essentially a simple custom Google Form that will post to a response Spreadsheet. 我正在开发一个Chrome扩展程序,它实际上是一个简单的自定义Google表单,会发布到响应电子表格。 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 the cors request headers because I was getting a No 'Access-Control-Allow-Origin' warning that blocked my request. 我必须包含cors请求标头,因为我收到了阻止我的请求的No'Access-Control-Allow-Origin'警告。

It being an extension, I also added the following permissions to the manifest.json file: 它是一个扩展,我还向manifest.json文件添加了以下权限:

"permissions": [
  "http://docs.google.com",
  "https://docs.google.com",
  "https://*.google.com",
]

At this point, I'm not sure exactly what's preventing the data from posting. 此时,我不确定究竟是什么阻止了数据的发布。 Possible indicators could be that when submitting the form I'm getting a "Provisional Headers are shown" caution and the server is taking way too long to respond as indicated by the Waiting (TTFB) time. 可能的指标可能是,在提交表单时,我会收到“临时标题显示”的警告,并且服务器花费太长时间来响应,如等待(TTFB)时间所示。

Where am I going wrong in the code? 我在代码中哪里出错了? (It did work once, for some reason.) Any alternative solutions out there to post a custom form to Spreadsheets? (出于某种原因,它确实工作过一次。)有任何替代解决方案可以将自定义表单发布到电子表格吗?

This is the way I did it... http://jsfiddle.net/adutu/7towwv55/1/ You can see that you receive a CORS error but it works... the data gets where it should be 这就是我这样做的方式... http://jsfiddle.net/adutu/7towwv55/1/你可以看到你收到一个CORS错误但是它有效...数据到达它应该的位置

        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
                }
            }
        });
        }

See more info here 在这里查看更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM