gpt4 book ai didi

ajax - 从网站表单写入谷歌文档时遇到 CORS 错误

转载 作者:行者123 更新时间:2023-12-01 13:53:33 26 4
gpt4 key购买 nike

我的网站上有一个表单,应该将文本字段“电子邮件”写入谷歌电子表格。
提交表单时,它会调用一个函数,该函数将 jquery ~ v 1.4 ajax POST 发送到谷歌文档。 (我已经测试了谷歌表单并且它有效)

我遇到了 CORS 错误。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://docs.google.com/forms/*************************************************/formResponse. This can be fixed by moving the resource to the same domain or enabling CORS.


  • 我显然无法将 google 文档移动到同一个域。
  • 我没有服务器的配置控制。
  • 我不想进行任何会影响的全局 angularjs 更改
    站点上使用的许多其他 ajax 和 $httpProvider。
  • 我不想将谷歌表单作为 iframe 嵌入网站中。

  • 我的 Ajax 是
    $scope.storeEmail = function() {
    var email = $scope.fA.email;
    $.ajax({type: "POST",
    async: true,
    url: 'https://docs.google.com/forms/***************************/formResponse',
    data: { field_key : email, submit : "Submit"},
    success: function(resp) {
    // give success feedback and redirect page
    }
    error: function(xhr, statusText, error){
    console.log(xhr);
    }
    };

    我试过
    向 ajax 请求添加凭据和 header
    withCredentials: true,
    headers: {
    'Content-Type': 'application/json; charset=utf-8'
    }

    但是我不能(至少我认为我不能)设置“AccessControlAllow*”参数。

    作为注释设置 withCredentials 本身似乎没有什么不同。
    设置 header 或使用其他类型(例如 PUT)会导致“NetworkError: 405 Method Not Allowed”响应。

    我尝试使用以下配置 angularjs
    App.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }
    ]);

    但是,我是 angularjs 的新手,我不知道这是否已放入正确的文件中,或者它是否会对站点上的其他(内部)$httpProvider 使用产生影响。

    有人告诉我,我需要为来自我的服务器的所有 HTTP 响应添加一个“Access-Control-Allow-Origin:*” header 。但由于服务器是我的谷歌文档服务器,我认为我无法做到这一点。

    任何帮助请。

    最佳答案

    我知道的跨域请求的两个选项是 jsonp 和 CORS。 CORS - 正如您已经注意到的 - 要求托管服务器在响应中包含 CORS header 。从它的声音来看,谷歌文档网站没有。

    不过,Google 确实在工作表和文档中提供了各种 jsonp 选项,因此不会丢失所有内容 - https://developers.google.com/gdata/samples/spreadsheet_sample

    您可以使用自己的服务器作为中间人,甚至可以使用 cors 代理等服务将所需的 header 添加到您请求的 google 内容中,而无需支付带宽费用。

    http://www.corsproxy.com/
    http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

    关于ajax - 从网站表单写入谷歌文档时遇到 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23956285/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com