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

What is the process of emphasizing a WebElement in WebdriverIO?

Is there a way to highlight web elements that need to be interacted with or asserted in webdriverIO? Or is there a JavaScript code similar to the javascript executor in Selenium that can be used for this purpose? ...

What is the method for incorporating an upward arrow into a select element, while also including a downward arrow, in order to navigate through options exclusively with

I have a select element that I have styled with up and down arrows. However, I am seeking assistance to navigate the options dropdown solely using these arrows and hide the dropdown list. Here is the HTML: <div class="select_wrap"> <d ...

mastering the chrome api within your redux-saga workflow

Currently, I'm working on creating a 'get_current_url' function using redux-saga in my project involving a Chrome extension built with React.js. In order to obtain the current URI, I need to utilize the chrome.tabs.query API. Below is the sn ...

Enhancing VueJS2 components by optimizing code structure to eliminate duplicate elements

The following code is used in two different components, so please avoid using props. Utilize data variables and largely similar methods but with different component templates. <template> </template> <script> export default { name ...

The challenge of accessing controllerAs variables/objects in AngularJS custom directives

After deciding to refactor my code and move DOM manipulation and functions into directives instead of controllers, I encountered an issue with accessing variables/objects defined using the controllerAs 'this' syntax. Despite trying to use bindToC ...

Is there a way to use JavaScript or jQuery to automatically convert HTML/CSS files into PDF documents?

While using OS X, I noticed that in Chrome I have the option to Save as PDF in the print window by setting the destination to "Save as PDF". Is this feature available on Windows? Is there a way to easily save to PDF with just one click? How can I access t ...

In express.js, cookies automatically expire upon the browser being closed

When working with express.js, I typically set cookies using the command: res.cookie("name", "value"); However, upon closing and reopening the browser, I noticed that the cookie is no longer there. Could someone advise me on how to mak ...

Can you explain the meaning of arguments[0] and arguments[1] in relation to the executeScript method within the JavascriptExecutor interface in Selenium WebDriver?

When utilizing the executeScript() method from the JavascriptExecutor interface in Selenium WebDriver, what do arguments[0] and arguments[1] signify? Additionally, what is the function of arguments[0] in the following code snippet. javaScriptExecutor.ex ...

Adding elements in increasing order based on the array values

I am implementing selectize.js, where the data is added through an array. My goal is to display the dropdown list in the order of DISPLAYORDER defined within the object. The code below generates the lists: option: function(item, escape) { var popular ...

Can the combination of pure HTML+JS applications with .NET webservices be both feasible and safe?

Our recent projects have utilized MVC5 with AngularJS, Ninject, Bootstrap, and various other technologies. However, we found that these applications required significant time to fix bugs and add features due to unnecessary complexity. Would it be feasible ...

What is the best way to ensure that the execution of "it" in mocha is paused until the internal promise of "it" is successfully resolved?

const promise = require('promise'); const {Builder, By, Key, until} = require('selenium-webdriver'); const test = require('selenium-webdriver/testing'); const chai = require('chai'); const getUrl = require('./wd ...

Bootstrap: Display a single product on the Carousel Product Slider for the smallest view

I found an example I'm using at this link: I noticed that when I resize my BrowserWindow, the boxes start to shrink. However, when the width reaches about 990px, the single products are rearranged in a 4-block layout from the initial width. Is there ...

What are some strategies for postponing the execution of a basic function for an

Whenever I develop microservices, I often come across situations where one function contains multiple other functions. For instance: functionA(); functionB(); functionC(); return json({status: processed}); All the functions within this block are synchro ...

Transferring data from a child component to a parent component in Vue without the need for a click

In my Vue project, I have a parent component called ChangeInfo which contains a child component named ShowWorkInfo. The ShowWorkInfo component includes several input forms for updating job information. To manage the data input, I created an object variable ...

Styling of Bootstrap HTML element not appearing as expected

Recently, I've been trying out a new approach by embedding Bootstrap components in an iframe. However, despite loading the necessary stylesheet and scripts, the elements seem to be missing their styles. Can anyone shed some light on why this might be ...

Access a dropdown menu by right-clicking in a Vue3 application integrated with Vuetify3

Currently, I am delving into the context menu functionality in Vue3. My aim is to display a list of items under the v-menu component when a user right-clicks on a dropdown button. For guidance, I am referring to this link. However, replicating the same in ...

Is there a way to ensure that an external file function completes before proceeding with the main code execution?

In my project, I have two key JavaScript files: main.js and load.js. Within load.js, there are two essential functions - getData, which makes an AJAX post call, and constHTML, which appends data based on the JSON array returned from the AJAX call. This app ...

Issue with AJAX MVC HTML: jQuery value is not blocking API call

Upon clicking a button, I am triggering a web API call using AJAX. My form is utilizing JqueryVal for form validations based on my viewmodel data annotations. The issue I am facing is that when I click the "Listo" button in my form, it executes the API ca ...

The concept of overloading operators in V8

Here's a question that I've been pondering, but couldn't seem to find any answers for on Google. While it may not be achievable in pure JavaScript, let's say I'm developing a container class in V8 and then returning that class bac ...

Disabling ESLint errors is not possible within a React environment

I encountered an eslint error while attempting to commit the branch 147:14 error Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions I'm struggling to identify the issue in the code, even ...