STG
STG

Reputation: 11

Google apps script, web app invokes CORS policy

I've been working on a Website, and wanted to use Google Sheets as my database for some god-aweful reason. In the Google Apps Script, I have a switch statement within the doGet method to get the path info. Like so:

function doGet(e)
{
   let output = "";
   path = e.pathInfo.split("/");
   switch(path[0])
   {
      case "Employees":
         if(path.length > 1)
         {
            output = getEmployeeByName(path[1]);
         }
         else
         {
            output = getAllEmployees();
         }
      break;
      case "Tasks":
         output = getAllTasks();
      break;
   }
   return ContentService.createTextOutput(JSON.stringify(output)).setMimeType(ContentService.MimeType.JSON);
}

Then, in my front end, using React.js, I try to fetch the URL link taken from the deployment.

 try
 {
  fetch(this.url +"/Employees", {
    redirect: "follow",
    method: "GET",
    headers: {
      "Content-Type": "text/plain;charset=utf-8",
    },
  })
 }
 catch (err)
 {
  console.log(err);
  return "Something messed up: ";
 }

The problem is that this above call gets blocked by CORS policy, but removing the extra path (And just calling fetch(this.url)) doesn't. I'm not entirely sure how to fix CORS errors, but I can confirm everything else works (Using the url and path in a web browser returns the right data).

Upvotes: 1

Views: 41

Answers (0)

Related Questions