javascript 如何通过 jQuery 和 Ajax 将 Google 表单数据发布到电子表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29267773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:18:04  来源:igfitidea点击:

How to Post Google Forms Data via jQuery and Ajax to Spreadsheets

javascriptajaxformsgoogle-chrome-extensiongoogle-sheets

提问by corcovado

I'm working on a Chrome extension that's essentially a simple custom Google Form that will post to a response Spreadsheet. I got the following function to successfully send and populate data only once, but never again:

我正在开发一个 Chrome 扩展程序,它本质上是一个简单的自定义 Google 表单,它将发布到响应电子表格。我得到了以下功能,只成功发送和填充数据一次,但再也不会:

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?

我在代码中哪里出错了?(由于某种原因,它确实工作过一次。)有没有其他解决方案可以将自定义表单发布到电子表格?

回答by adutu

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

在此处查看更多信息