首页 > javascript > 使用AJAX将数据发布到Google表单

使用AJAX将数据发布到Google表单 (POST data to a Google form with AJAX)

问题

我正在尝试将数据发布到从AJAX到Google表单,但即使它报告statusCode为0的成功,数据也不会出现在表单中:

var dat={ "entry.529474552" :"data1", "entry.1066559787": "name1"};
postToGoogle("1FAIpQLSf4w1OQGsIncaiqXlmfAl4jYSt-e4Zx3xVJa7Weob4LnQbRZQ",dat);

function postToGoogle(id, dat) {
    $.ajax({
        beforeSend: function (xhr) {
            xhr.setRequestHeader('Access-Control-Allow-Origin', 'chrome-extension://EXTENSION_ID');
            xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, PUT');
            },
        url: "https://docs.google.com/forms/d/e/"+id+"/formResponse",
        data: dat, 
        type: "POST",  
        dataType: "xml",
        xhrFields: {  withCredentials: true },
        statusCode: {
            0:   function() {  console.log("OK") },
            200: function() {  console.log("error") },
            }
        });
    }

它会产生一个CORS错误,但据推测,POST应该会通过。

解决方法

对于那些希望在2019年4月这样做的人

我今天正在研究这个问题。目前的答案如下:

Google表单中的每个字段都有一个唯一的ID。要检索它,请填写您希望以编程方式提交的所有相关表单字段,以获取预填充链接

注意:为了确保无限制地访问URL,您还需要仅对您的域禁用限制(对于GSuite付费用户)

Google表格界面

然后,复制链接并粘贴到浏览器中。URL的基本格式如下:

https://docs.google.com/forms/d/e/[FORMID]/formResponse

每个参数都是一个键/值对,类型为:entry.XXXXXX = value

预填充链接将为每个相关字段提供XXXXX的值

请求必须使用以下标头:

方法: GET

内容类型: application / x-www-form-urlencoded

最终请求看起来像这样

https://docs.google.com/forms/d/e/123332233bsj333/formResponse?entry.123456=value1&entry.2333442=value2&submit=Submit

不要忘记在您的请求结束时附上submit = Submit!

问题

I am trying to post data to a from AJAX to a Google form, but even though it reports success with a statusCode of 0, the data never appears in the form:

var dat={ "entry.529474552" :"data1", "entry.1066559787": "name1"};
postToGoogle("1FAIpQLSf4w1OQGsIncaiqXlmfAl4jYSt-e4Zx3xVJa7Weob4LnQbRZQ",dat);

function postToGoogle(id, dat) {
    $.ajax({
        beforeSend: function (xhr) {
            xhr.setRequestHeader('Access-Control-Allow-Origin', 'chrome-extension://EXTENSION_ID');
            xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, PUT');
            },
        url: "https://docs.google.com/forms/d/e/"+id+"/formResponse",
        data: dat, 
        type: "POST",  
        dataType: "xml",
        xhrFields: {  withCredentials: true },
        statusCode: {
            0:   function() {  console.log("OK") },
            200: function() {  console.log("error") },
            }
        });
    }

It generates a CORS error, but supposedly, the POST should go through anyway.

解决方法

For those looking to do this as of April 2019

I was working on this today. The current answer is as follows:

Each field in the Google Form has a unique ID. In order to retrieve it, get a pre-filled link by filling out all relevant form fields you wish to programatically submit

Note: In order to ensure the URL is accessible without restriction, you'll also want to disable restriction to your domain only (for GSuite paying users)

Google Forms Interface

Then, copy the link and paste in your browser. The URL has a base format as follows:

https://docs.google.com/forms/d/e/[FORMID]/formResponse

Each param is a key/value pair of type: entry.XXXXXX=value

The prefilled link will give you the value of XXXXX for each relevant field

Request must be made with following headers:

Method: GET

Content-Type: application/x-www-form-urlencoded

The final request looks like this

https://docs.google.com/forms/d/e/123332233bsj333/formResponse?entry.123456=value1&entry.2333442=value2&submit=Submit

Don't forget to append submit=Submit at the end of your request!

相似信息