通过AJAX向Google表单发送数据

Sending data to Google Forms via AJAX

本文关键字:数据 表单 Google AJAX 通过      更新时间:2023-09-26

我想通过AJAX请求向Google表单发送数据。

我已经设置了一个谷歌表单,当我从一个直接链接使用它时,它工作得很好。

但是,当我加载下面的页面:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Hello Google Forms</title>
  </head>
  <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js">
    </script>
    <script>
      // entry ids
      var entry_id_email = "entry.XXX";
      var entry_id_name  = "entry.XXX";
      // data to be sent
      var data = {};
      data[entry_id_email] = "foo@bar.com";
      data[entry_id_name]  = "Foo Bar";
      // form key
      var form_key = "XXX";
      // url
      var url = "https://docs.google.com/forms/d/" + form_key + "/formResponse";
      // ajax request
      $.ajax({
        url: url,
        data: data,
        type: "POST",
        dataType: "xml",
      });
    </script>
  </body>
</html>

我得到以下错误:

XMLHttpRequest cannot load https://docs.google.com/forms/d/XXX. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://myorigin' is therefore not allowed access.

错误提示您正在使用跨域ajax请求。因此需要将attype从xml更改为jsonp。这将修复表单提交问题。但是你会得到以下错误:

拒绝执行来自'https://docs.google.com/forms/d/key...ion%201&callback=jQuery111307080722744576633_1445478442168&_=1445478442169'的脚本,因为它的MIME类型('text/html')不可执行,并且启用了严格的MIME类型检查。

此问题无法修复,因为更新了跨域JSONP XML响应。