CORS error while writing to Google documents from a site form

I have a form on my website that should write an "email" text box to a google spreadsheet. When the form submits, it calls a function that submits jQuery ~ v 1.4 ajax POST to google docs. (I tested the Google form and it works)

I encountered a CORS error.

Cross-request request is blocked: a policy of the same origin prohibits reading a remote resource at https://docs.google.com/forms/ ************************ * ************************* / formResponse. This can be fixed by moving the resource to the same domain or by including CORS.

  • I obviously cannot transfer the google document to the same domain.
  • I do not have server configuration control.
  • I do not want to make any global angle changes that many other ajax and $ httpProvider use on the site could affect.
  • I do not want to embed the google form as an iframe on the site.

My 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 tried adding credentials and headers to ajax request

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

However, I cannot (at least I do not think I can) set the AccessControlAllow * parameters.

As a side note, setting withCredentials doesn't seem to matter on its own. Setting headers or using other types, such as PUT, results in a "NetworkError: 405 Method Not Allowed" response.

I tried to adjust the angular values ​​with the following

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 placed in the correct file or if it would have a knock effect on the other (internal) $ httpProvider that is used on the site.

, "Access-Control-Allow-Origin: *" HTTP . - Google, , .

.

+4
1
0

Source: https://habr.com/ru/post/1542658/


All Articles