Google Apps Scripts implementation functions successfully in Postman but encounters issues when accessed from JavaScript code

Having created a script for a Google Sheet in Apps Script, I defined it as follows:

function doPost(e) {
  var app = SpreadsheetApp.getActive();
  var sheet = app.getSheetByName('Web');

  var content = JSON.parse(e.postData.contents)
  sheet.getRange("A1").setValue(content);

  return ContentService.createTextOutput("Saved");
}

After implementing it as a Web Application and setting the access to Anyone, I successfully sent post requests through Postman to the generated URL without any issues. The request body content was consistently stored in the "A1" cell. https://i.stack.imgur.com/rLrO8.png https://i.stack.imgur.com/pfbBj.png

However, when attempting to send the exact same request using the Fetch API from my Vue.js web application, it fails with a TypeError: Failed to fetch error, providing no additional information such as status codes.

The function called on button click from my web app is as follows:

function sendForm() {

    var myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");

    var raw = JSON.stringify({
        "test": "testing from vue"
    });

    var requestOptions = {
        method: 'POST',
        headers: myHeaders,
        body: raw,
        redirect: 'follow'
    };

    fetch("https://script.google.com/macros/s/<id of the implementation>/exec", requestOptions)
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error));
}

Even though the JavaScript code was generated by Postman itself, the issue persists. The console output shows: https://i.stack.imgur.com/2CRxm.png

I also tried using axios, but encountered a similar issue with a different error message: Network Error. https://i.stack.imgur.com/jgSXk.png

Seeking guidance on what steps to take or where to look next, as hours of online research have not provided a solution. Any insights would be greatly appreciated.

Answer №1

After successfully sending a post request through Postman to the provided URL, I have verified that your Web Apps are functioning correctly. It seems like your script is working as expected, but have you considered making the following modifications?

Revised script:

function sendForm() {
  var raw = JSON.stringify({"test": "testing from vue"});
  var requestOptions = {
    method: 'POST',
    body: raw,
  };
  fetch("https://script.google.com/macros/s/<id of the implementation>/exec", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
}
  • Upon testing this updated script with your Web Apps script, it was confirmed that {test=testing from vue} is successfully inserted into the designated cell.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message: "The variable _ is not defined when trying to use angular-google-maps library"

I'm encountering an issue where I'm receiving a ReferenceError: _ is not defined while using the angular-google-maps I'm puzzled as to why this error is occurring, as I believe I am following the instructions provided on the website. I hav ...

Adjust the styling of the anchor tag within the selected div by utilizing jQuery

I am struggling with changing the color of a specific anchor tag when clicked inside a div with class .product_wishlist. Currently, all anchor tags within .pw div are changing colors. Is there a way to apply selected css only to the clicked element? Any he ...

PHP is unable to decode JSON that has been converted from JavaScript

When I send an array as a POST request, I first convert it to JSON using the JSON.stringify() method. However, I encountered an issue when trying to decode it in PHP. // JavaScript var arr1 = ['a', 'b', 'c', 'd', & ...

How to reference an object from an external file in TypeScript using Ionic 2 and Angular 2

I am currently developing a mobile application with Ionic2 and have integrated a simple online payment service called Paystack for processing payments. The way it operates is by adding a js file to your webpage and then invoking a function. <script> ...

Unable to locate module post npm installation

My Node.js application is running on an Ubuntu server hosted on Microsoft Azure. The package.json file for my project includes various dependencies required for the app to function. { "author" : "Coop", "name" : "app-framework", "main" ...

Execute a post request upon clicking with NEXT JS, while also firing off a get request

I'm facing an issue where I need to post and get my data when clicking on the same button (similar to writing and displaying comments). However, whenever I click the button, everything seems to be working fine but a request with a 304 status code star ...

Ways to ensure the text on my website scrolls into view as the user navig

Is there a way to have my text show up as I scroll? I came across this , but I'm interested in how it actually functions. I saw a similar inquiry before, but it doesn't make sense to me. Can someone please explain or provide some examples on how ...

Having trouble parsing an XML received from AJAX request

I am encountering an issue with the code below: $.ajax({ type: "POST", dataType: "xml", url: getUrl('/GetPeriodicStats/'), data: XML.innerHTML,//some xml, success: function(c) { After receiving the XML data in the clie ...

Refresh the page before the conclusion of the express-Node js function

I am encountering an issue with a function that functions properly with small files but fails when dealing with large files. The problem occurs when the axios post request in Express JS ends, causing a page refresh. I am using React JS on the client side a ...

What steps can I take to stop my browser from displaying the "waiting for MyHostName" message while performing an ajax Post/Get operation?

Whenever I access a website using Chrome, a message appears in the status bar saying "Waiting for MyHost name" along with an Ajax Loader circle in the browser tab. Here is a javascript function that I am working with: function listen_backend_client_reques ...

AngularJS Login Popup with SpringSecurity

I have successfully integrated spring security with my AngularJS webpage utilizing Rest API. However, I am facing an issue where every time I attempt to log in using the rest api from my customized login page, it prompts me for the login credentials in a p ...

Struggling to construct a project using parcel, continually encountering issues with unsupported file types

My attempt at creating a project using parcel has hit a snag. Despite diligently following the guidelines provided in my assignment, an error message consistently appears in my terminal each time I initiate the command: parcel src/index.html The error mes ...

Creating a Custom Validator in Vuejs that can verify each attribute of an object within the props

I have an object called car in my props, and I need to validate each attribute of this object. Here are the rules: color must be either white or black name must be a String numberOfSits should be between 2 and 4 status must be either Available or Not Avai ...

Looking for the properties of the ServerResponse object in node.js - where can I locate them?

Currently, I've been diving into the book Node.js In Action. Chapter 9 introduces a message module with a function that has left me puzzled about the 'this' object when calling res.message. To gain clarity, I decided to print out the name of ...

Navigating rows in a Kendo Grid using buttons (Starting, Previous, Next, Ending)

Looking for help with navigating rows on a Kendo-Grid. I have four Buttons - First, Previous, Next, and Last. The goal is to highlight the selected record on the grid when a button is clicked. However, I'm struggling with making the Kendo-Grid highlig ...

What is the process of accessing JSON data transmitted from a Express server in JavaScript?

Working with a node server, I've set up a client-server model, meaning the client connects to "localhost" and express generates a response based on certain logic. While I'm able to send a JSON object through the response, I'm unsure of how t ...

Encountering a React clash while integrating Material UI

Upon trying to utilize the AppBar feature in Material UI version 0.16.6, I encountered the following error: Uncaught Error: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created within a c ...

Storing user data in node.js using the express-sessionTo save user data in

Using express and express-session with mysql on nodeJS has been successful for me. I managed to set up a cookie and session as well. Take a look at my code: app.use(cookieParser('3CCC4ACD-6ED1-4844-9217-82131BDCB239')); session({resave: true, s ...

Exploring the mechanics behind ES6 Map shims

From what I've gathered from the documentation (here and here), it seems that having a reference to the memory address is necessary for the operation to work: const foo = {}; const map = new Map(); map.set(foo,'123'); // This action requi ...

In React production, the use of .map as a function is unavailable

While my express react app build runs smoothly locally with the map function, the transition to production triggers an error stating that 'map' is not a function. The error specifically points to the line declaring 'let returnlist'. Be ...