首页 > 解决方案 > UrlFetchApp 和 doPost(e),相同的代码,不同的项目,不同的行为

问题描述

正如我在标题中所说,我在前端和后端测试项目(前端:UrlFetchApp| 后端:)doPost(e)和开发项目上得到了相同的代码。

由于我是 Google Apps 脚本编程的新手,我在一个 gmail 帐户中做了一个测试项目,以验证两个不同电子表格之间的 fetchUrlApp 和 doPost 通信等过程。开发项目在另一个帐户上用作预生产项目。

后端脚本在测试和开发项目中都部署为 Web 应用程序新版本发布方法是在测试项目上加载示例基本 JSON 我在后端获得我的 JSON,但在开发项目上没有。

这是代码和结果

后端doPost代码:

function doPost(e) {
    Logger.log("I was called");
    if (typeof e != 'undefined');
    var data = JSON.parse(e.postData.contents);
    var ss = SpreadsheetApp.openById("my-backend-id");
    var sheet = ss.getSheetByName("Log");
    sheet.getRange(1, 1).setValue(JSON.stringify(data))
    //Logger.log(JSON.stringify(data));
    return ContentService.createTextOutput(JSON.stringify(e));
};

前端提交帖子请求:

function test_rapide(){
  var data = {"id":"xyz","name":"xy"};
  var ret = makeRequest("https://my-web-app-URL/exec", data);
    
  var content = ret.getContentText();
  Logger.log(content);
};

测试项目成功的结果:

frontend log:
2020-07-03 14:31:33:364 +1100 003093 INFO {"contextPath":"","queryString":"","postData":{"contents":"{\"id\":\"xyz\",\"name\":\"xy\"}","length":24,"name":"postData","type":"application/json"},"parameters":{},"parameter":{},"contentLength":24}

backend log:
2020-07-03 14:31:32:148 +1100 000625 INFO I was called
2020-07-03 14:31:32:322 +1100 000799 INFO [object Object]
2020-07-03 14:31:32:901 +1100 001378 INFO {"id":"xyz","name":"xy"}

开发项目的结果失败:

frontend log:
"2020-07-03 15:05:13:886 +1100 000733 INFO Drive Sorry, unable to open the file at this time.Please check the address and try again.Get stuff done with Google Drive Apps in Google Drive make it easy to create, store and share online documents, spreadsheets, presentations and more. Learn more at drive.google.com/start/apps."
backend log:
nothing```

标签: google-apps-scriptweb-applicationsurlfetch

解决方案


推荐阅读