使用嵌入式谷歌表单绕过 CORS [英] Getting around CORS with embedded google forms

查看:35
本文介绍了使用嵌入式谷歌表单绕过 CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过嵌入式表单将表单数据发送到谷歌.

I'm trying to send form data to google via an embedded form.

我发现这个帖子似乎回答了我的问题,但我收到 CORS 错误.有没有办法解决这个问题?

I found this post that seems to answer my question but I'm getting CORS errors. Is there a way to solve this?

其他帖子似乎说 CORS 不是问题,但我收到了错误.

Other posts seem to say that CORS isn't an issue but I'm getting the errors.

这是我的代码:

-JS-

function ajax_post() {
  var field1 = $('#email').val();

  $.ajax({
      url: "https://docs.google.com/forms/d/e/xxxxxxxxxxxxxxxxxxxxxx/formResponse",
      data: {"entry.xxxxxxxxxx": field1},
      type: "POST",
      dataType: "xml",
      statusCode: {
          0: function() {
          //Success message
          },
          200: function() {
          //Success Message
          }
      }
  });
}

-HTML-

<form id="emailForm" target="_self" onsubmit="" action="javascript: ajax_post()">
    <input id="email" type="text" autocomplete="off" tabindex="0" name="entry.xxxxxxxxxx" required>
    <button id="send" type="submit">Submit</button>
</form>

推荐答案

请求的资源上不存在 'Access-Control-Allow-Origin' 标头" 消息表示来自 https://docs.google.com/forms/d/e/xxxx/formResponse 网址目前不包含 Access-Control-Allow-Origin 响应标头,因此浏览器不允许您的前端 JavaScript 代码访问响应.

The "No 'Access-Control-Allow-Origin' header is present on the requested resource" message indicates that responses from https://docs.google.com/forms/d/e/xxxx/formResponse URLs currently don’t include the Access-Control-Allow-Origin response header, so browsers won’t allow your frontend JavaScript code to access the response.

鉴于此,我猜从您的前端代码中您无法判断 POST 请求是否成功.但是,除非出现任何其他问题,请求似乎总是会成功.如果请求根本没有到达服务器(由于某些网络错误),那么您将遇到不同的失败情况,该情况可从前端代码中观察到,以便您可以实际捕获它.

Given that, I guess from your frontend code there’s no way you can tell whether the POST request succeeds or not. But barring any other problems, it seems like the request will always succeed. If the request doesn’t reach the server at all (due to some network error) then you’ll hit a different failure condition that is observable from your frontend code so you can actually catch it.

所以我想您知道请求已成功到达服务器的方式只是您没有从前端代码中观察到任何其他故障.

So I guess the way you know the request has successfully reached the server is just that you don’t get any other failure that’s observable from your frontend code.

这篇关于使用嵌入式谷歌表单绕过 CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆