最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - POST data to a Google form with AJAX - Stack Overflow

programmeradmin3浏览0评论

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: "/"+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.

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./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.

Share Improve this question edited Aug 24, 2018 at 12:45 Bill Ferster asked Aug 23, 2018 at 22:23 Bill FersterBill Ferster 3471 gold badge5 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

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)

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

https://docs.google./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./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 (i.e. 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.

发布评论

评论列表(0)

  1. 暂无评论