简体   繁体   中英

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:

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表单界面

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!

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

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.

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)
  • Credentials (ie you have set withCredentials: true )

… it is a Preflighted Request.

Before the browser will make the POST request, it will make an OPTIONS request to ask permission.

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 . You can't read the status when there is a CORS error.

Much better option is: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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