简体   繁体   English

使用AJAX将数据发布到Google表单

[英]POST data to a Google form with AJAX

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: 我试图将数据从AJAX发布到Google表单,但是即使状态码为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") },
            }
        });
    }

It generates a CORS error, but supposedly, the POST should go through anyway. 它会产生CORS错误,但据推测,无论如何,POST应该都会通过。

For those looking to do this as of April 2019 对于那些希望在2019年4月之前做到这一点的人

I was working on this today. 我今天正在做这个。 The current answer is as follows: 当前答案如下:

Each field in the Google Form has a unique ID. Google表单中的每个字段都有唯一的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) 注意:为了确保可以不受限制地访问URL,您还希望仅对您的域禁用限制(对于GSuite付费用户)

Google表单界面

Then, copy the link and paste in your browser. 然后,复制链接并粘贴到浏览器中。 The URL has a base format as follows: 该URL的基本格式如下:

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

Each param is a key/value pair of type: entry.XXXXXX=value 每个参数都是类型的键/值对:entry.XXXXXX = value

The prefilled link will give you the value of XXXXX for each relevant field 预填充的链接将为您提供每个相关字段的XXXXX值

Request must be made with following headers: 必须使用以下标头进行请求:

Method: GET 方法: GET

Content-Type: application/x-www-form-urlencoded 内容类型: 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 https://docs.google.com/forms/d/e/123332233bsj333/formResponse?entry.123456=value1&entry.2333442=value2&submit=提交

Don't forget to append submit=Submit at the end of your request! 不要忘了在请求末尾附加Submit = Submit!

It generates a CORS error, but supposedly, the POST should go through anyway. 它会产生CORS错误,但据推测,无论如何,POST应该都会通过。

While it is possible to make a successful POST request, get a CORS error, and be unable to read the response, this is only true for Simple Requests. 尽管有可能发出成功的POST请求,出现CORS错误并且无法读取响应,但这仅适用于简单请求。

Because your request has: 因为您的请求具有:

  • Custom headers ( Access-Control-Allow-Origin and Access-Control-Allow-Methods , which are response headers and have no business being on a request in the first place) 自定义标头( Access-Control-Allow-OriginAccess-Control-Allow-Methods ,它们是响应标头,一开始就没有业务在请求中)
  • Credentials (ie you have set withCredentials: true ) 凭证(即,您已设置withCredentials: true

… it is a Preflighted Request. …这是一个预检请求。

Before the browser will make the POST request, it will make an OPTIONS request to ask permission. 在浏览器发出POST请求之前,它将发出OPTIONS请求以寻求许可。

Since it doesn't get permission, the request fails. 由于未获得许可,因此请求失败。


Note that even if you did turn it into a simple request and make the POST successfully, you would still get a status of 0 . 请注意,即使您确实将其转换为简单请求并成功进行POST,您仍将获得状态0 You can't read the status when there is a CORS error. 发生CORS错误时,您无法读取状态。

Much better option is: https://github.com/jsdevel/google-form 更好的选择是: https : //github.com/jsdevel/google-form

But I have a question how to customize after submit screen, with message : Thank you, your responce has been sent. 但是我有一个问题,在提交屏幕后如何自定义,并显示消息:谢谢,您的回复已发送。

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

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