我正在尝试调用应用程序脚本部署的 Web 应用程序(doPost 方法)并获取返回客户端应用程序的响应(这可能是 HTTP 客户端,如 Postman/Bruno 或简单的 javascript 应用程序)。 部署的 Web 应用程序将调用外部 API 并返回其结果。 当我尝试此操作时,我收到一条消息 (HTML),显示“脚本已完成,但返回的值不是受支持的返回类型”。 如果我不调用外部 API 并尝试从 doPost 返回默认/硬编码对象,则客户端将正确获取响应(作为 JSON)。 控制台日志语句显示正确的输出值 请大家指点一下有没有出路。
部署的网络应用程序(应用程序脚本)是
let respout= { "jokeid": 0, "errcode": 0, "errmsg": "na" };
function doPost()
{
console.log('inside doPost func', respout);
try
{ //If tried to return above respout with out executing "jokefun()", below statement works with client
//return ContentService.createTextOutput(JSON.stringify(respout)).setMimeType(ContentService.MimeType.JSON);
return jokefunc().then( res => {
respout={ "jokeid": res.id,"errcode": 200,"errmsg": "success" };
console.log('resp out val in then func',respout);
return ContentService.createTextOutput(JSON.stringify(respout)).setMimeType(ContentService.MimeType.JSON);
})
}
catch (error)
{
console.log('Exception in doPost', error);
}
}
async function jokefunc()
{
try
{
const API_URL = "https://official-joke-api.appspot.com/random_joke";
const response = UrlFetchApp.fetch(API_URL);
const responseText = response.getContentText();
const jsonData = JSON.parse(responseText);
console.log('Fetch called', jsonData);
return jsonData; // Return the fetched data object
}
catch(error)
{
console.error('Some thing went wrong',error)
}
}
人们不在乎。我得到了解决方案。 我只是尝试删除异步并在 doPost 中以同步方式调用,它的工作方式就像一个魅力。