通过AJAX将数据发送到Google表单

时间:2015-10-22 00:51:00

标签: javascript jquery ajax xmlhttprequest google-form

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

我已经设置了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.

1 个答案:

答案 0 :(得分:0)

错误告诉您正在使用跨源ajax请求。因此,您需要将atatype从 xml 更改为 jsonp 。这将解决您的表单提交问题。但是你会得到以下错误:

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

此问题无法修复为udate Cross Domain JSONP XML Response