Search code examples
google-apps-scriptgoogle-chrome-extensioncorscross-domain

Access Google spreadsheet from Chrome Extension: CORS No 'Access-Control-Allow-Origin' header error


I have a Google spreadsheet and a Google script app that has method doGet(e) which returns spreadsheet content in JSON. Google app is published, and by calling URL I receive totally valid JSON i'm looking for.

Now, I'd like my Chrome extension to call that URL and receive all the data.

It's been working a while ago, but as of this moment, it crashes with an error

Access to XMLHttpRequest at 'https://script.google.com/macros/s/XXX/exec' from origin 'https://www.YYY.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I've been digging around for a while, tried several solutions, but it seems most of them are related to the backend, which I don't have access to (its still Google script app). Are there any workarounds?

EDIT: no luck with that option. My code (tried application/x-www-form-urlencoded, multipart/form-data, text/plain - got the same results):

$.ajax({    
  url:'https://script.google.com/macros/s/XXX/exec',
  headers: { 
    'Content-Type': 'application/x-www-form-urlencoded',
    'Access-Control-Allow-Origin': '*'
  },
  success:function (result) {
    *** 
  }

Solution

  • Thanks to Tanaike for a great hint!

    Apparently, the issue was in publishing settings. Basically, I did 2 steps to solve it:

    1. As mentioned by TheMaster, i've switched request ContentType to 'application/x-www-form-urlencoded'. For more details, refer to this answer.
    2. As mentioned by Tanaike, i've switched the publishing settings to Execute the app as: Me and Who has access to the app: Anyone, even anonymous.

    enter image description here