从网站表单写入Google文档时遇到CORS错误

时间:2022-08-22 10:06:25

I have a form in my website that should write a text field "email" to a google spreadsheet. When the form is submitted it calls a function that sends a jquery ~ v 1.4 ajax POST to google docs. (I have tested the google form and it works)

我的网站上有一个表单,应该将一个文本字段“email”写入谷歌电子表格。提交表单时,它会调用一个函数,将jquery~v 1.4 ajax POST发送到google docs。 (我测试了谷歌表格,它的工作原理)

I have encountered the CORS error.

我遇到了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.

跨源请求已阻止:同源策略禁止在https://docs.google.com/forms/**********************上读取远程资源*************************** / formResponse。这可以通过将资源移动到同一域或启用CORS来解决。

  • I obviously cannot move the google doc to the same domain.
  • 我显然无法将谷歌文档移动到同一个域。

  • I do not have configuration control of the server.
  • 我没有服务器的配置控制。

  • I do not want to make any global angularjs changes that would affect the many other ajax & $httpProvider uses on the site.
  • 我不想做任何会影响许多其他ajax和$ httpProvider在网站上使用的全局angularjs更改。

  • I do not want to embed the google form as an iframe in the site.
  • 我不想将谷歌表单作为iframe嵌入网站。

My ajax is

我的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);
        }        
};

I have tried Adding credentials and headers to the ajax request

我尝试过将凭据和标题添加到ajax请求中

withCredentials: true,
headers: {
  'Content-Type': 'application/json; charset=utf-8'
}

However I cannot (at least I don't think I can) set the 'AccessControlAllow*' parameters.

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

As a note setting withCredentials does not seem to make a difference on its own. Setting headers or using other types such as PUT results in a "NetworkError: 405 Method Not Allowed" response.

作为一个带有问题的笔记设置似乎没有自己的作用。设置标头或使用其他类型(如PUT)会导致出现“NetworkError:405 Method Not Allowed”响应。

I tried configuring angularjs with the following

我尝试使用以下配置angularjs

App.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
  }
]);

However I am new to angularjs and I don't know if this was put into the correct file or if it will have a knock on effect on the other (internal) $httpProvider uses on the site.

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

I have been told that I need to add an "Access-Control-Allow-Origin: *" header to all HTTP responses from my server. But as the server is my google documents server I don't think I can do this.

有人告诉我,我需要为我服务器的所有HTTP响应添加一个“Access-Control-Allow-Origin:*”标头。但由于服务器是我的谷歌文档服务器,我认为我不能这样做。

Any help Please.

任何帮助请。

1 个解决方案

#1


0  

The two options I'm aware of for cross domain requests are jsonp and CORS. CORS - as you've already noted - requires the hosting server to have CORS headers included in responses. By the sounds of it the Google documents site does not.

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

Google do have various jsonp options across sheets and document though, so all is not lost - https://developers.google.com/gdata/samples/spreadsheet_sample

谷歌确实在表格和文档中有各种jsonp选项,因此一切都不会丢失 - https://developers.google.com/gdata/samples/spreadsheet_sample

You could make use of your own server as a middle man, or even use a service such as cors proxy to add the headers you need to the google content you're requesting without having to pay for the bandwidth.

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

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

#1


0  

The two options I'm aware of for cross domain requests are jsonp and CORS. CORS - as you've already noted - requires the hosting server to have CORS headers included in responses. By the sounds of it the Google documents site does not.

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

Google do have various jsonp options across sheets and document though, so all is not lost - https://developers.google.com/gdata/samples/spreadsheet_sample

谷歌确实在表格和文档中有各种jsonp选项,因此一切都不会丢失 - https://developers.google.com/gdata/samples/spreadsheet_sample

You could make use of your own server as a middle man, or even use a service such as cors proxy to add the headers you need to the google content you're requesting without having to pay for the bandwidth.

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

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