D
D
Den Mokrinsky2020-08-15 12:57:17
Google Apps Script
Den Mokrinsky, 2020-08-15 12:57:17

How to get a response from a script called via POST / GET in the form of return?

I would like to pull the POST / GET script with a request and receive a response in the form of only a specific value, but I tried different return options and they return Html, is there a way to return the value like through a php file call?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Stoyanov, 2020-08-15
@den_mok

Perhaps you want to deploy your script as an api service?
If this is the case, then do the following:
1. Menu Publish > Deploy as a web application, in the settings, select on my behalf and for everyone, including anonymous users.
2. In the doPost or doGet function, return the ContentService, depending on what result you expect, such a result format and specify, for example, like this:

function doGet(e) {
  const result = {};
  
  // ваш код
  
  if ( e.parameter.callback ) {
    return ContentService
    .createTextOutput(`${ e.parameter.callback }(${ JSON.stringify(result) })`)
    .setMimeType(ContentService.MimeType.JAVASCRIPT);
  } else {
    return ContentService
    .createTextOutput(JSON.stringify(result))
    .setMimeType(ContentService.MimeType.JSON);
  }
}

Official documentation
https://developers.google.com/apps-script/guides/c...
Response types:
https://developers.google.com/apps-script/referenc...
---
Maxim Stoyanov (stomaks ), developer of Google Apps Script .
g-apps-script.com
stomaks.me

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question