从 Jive 响应中收到格式错误的 HTML 错误,我该如何继续?

Receiving malformed HTML error from Jive response, how can I proceed here?

目前,在尝试解析 JSON 以从 JIVE JSON 响应部署到应用程序脚本时,目前每一步都取得了成功。此时,我收到格式错误的 HTML 错误,我不太确定如何回应。我已经发布到 Jive 社区,但我认为没有人在那里工作了...

更新:

这是我用来在应用程序脚本中产生此问题的代码:

Code.gs

function doGet(request) { 
  return HtmlService.createTemplateFromFile('Page').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function include(filename) { 
  var finalRequest = UrlFetchApp.fetch('https://www.cloudconnect.xxx....');

  var data = finalRequest.toString().replace("throw 'allowIllegalResourceCall is false.';", "").trim(); 

  data = JSON.parse(data);

  var returnData = [];

  for(var i in data){
    if(i == "list"){
     var content = data[i];
    }
    for(var i in content){
     var text = content[i];
      var json_string = JSON.stringify(text).replace(/[&]/g, "and");
      returnData.push(json_string);
    }
  }
  return returnData;
}

Page.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <?!= include('Stylesheet'); ?>
  </head>
  <body>
  <script>
    function displayData(responseData) {
        document.getElementById('output').innerHTML = responseData.join(", ");
    } 
    google.script.run.withFailureHandler(displayData).withSuccessHandler(displayData).include();
    </script>
  </body>
</html>

发布到网络应用程序时,这是我得到的响应:

Exception: Malformed HTML content: {"content":{"text":"<body><!-- [DocumentBodyStart:c062b27a-e734-42fb-a146-c2d978fb77da] --><div class=\"jive-rendered-content\"></div><!-- [DocumentBodyEnd:c062b27a-e734-42fb-a146-c2d978fb77da] --></body>","editable":false,"type":"text/html"},"subject":"GCI - Partner Project Plan - Template","type":"file"},{"content":{"text":"<body><!-- [DocumentBodyStart:1248f967-2b32-42d0-a9aa-a20f40485c77] --><div class=\"jive-rendered-content\"><span>Unable to access trainingand#160;Google Cloud Sales Credential</span></div><!-- [DocumentBodyEnd:1248f967-2b32-42d0-a9aa-a20f40485c77] --></body>","editable":false,"type":"text/html"},"subject":"Unable to access training Google Cloud Sales Credential","type":"update"},{"content":{"text":"<body><!-- [DocumentBodyStart:67e6b807-8421-4e61-a3c9-a08943f8a368] --><div class=\"jive-rendered-content\"><span>I am having issues with Capital Letter Confusion ie - Example.Com and example.com.I have major account confusion as a result.Thanks for reading.</span></div><!-- [DocumentBodyEnd:67e6b807-8421-4e61-a3c9-a08943f8a368] --></body>","editable":false,"type":"text/html"},"subject":"I am having issues with Capital Letter Confusion ie - Example.Com and example.com.I have major...","type":"update"},{"content":{"text":"<body><!-- [DocumentBodyStart:0013baf2-c12e-4869-9849-0959d99bdd77] --><div class=\"jive-rendered-content\"><p>or how can I know who is the admin? if they no longer have any IT administrator and the last one they had quits two years ago with zero documentation?</p></div><!-- [DocumentBodyEnd:0013baf2-c12e-4869-9849-0959d99bdd77] --></body>","editable":false,"type":"text/html"},"subject":"Why a gsuite edu user shows that his admin is a domain instead of an name or email account?","type":"discussion"},{"content":{"text":"<body><!-- [DocumentBodyStart:907a55ec-9332-43fe-876c-00384af7be79] --><div class=\"jive-rendered-content\"><span>Startup FB Data Science University will launch in the San Francisco, CA and Cloud Tech developers as individuals or companies can be Partner of the Startup Tech University without capital.</span><a class=\"jive-link-external-small\" href=\"https://www.cloudconnect.goog/external-link.jspa?url=http%3A%2F%2Fbit.ly%2Ffbdatascienceuniversity\" rel=\"nofollow\" target=\"_blank\">xxx</a></div><!-- [DocumentBodyEnd:907a55ec-9332-43fe-876c-00384af7be79] --></body>","editable":false,"type":"text/html"},"subject":"Startup FB Data Science University will launch in the San Francisco, CA and Cloud Tech developers...","type":"update"},{"content":{"text":"<body><!-- [DocumentBodyStart:c062b27a-e734-42fb-a146-c2d978fb77da] --><div class=\"jive-rendered-content\"></div><!-- [DocumentBodyEnd:c062b27a-e734-42fb-a146-c2d978fb77da] --></body>","editable":false,"type":"text/html"},"subject":"GCI - Partner Project Plan - Template","type":"file"},{"content":{"text":"<body><!-- [DocumentBodyStart:1248f967-2b32-42d0-a9aa-a20f40485c77] --><div class=\"jive-rendered-content\"><span>Unable to access trainingand#160;Google Cloud Sales Credential</span></div><!-- [DocumentBodyEnd:1248f967-2b32-42d0-a9aa-a20f40485c77] --></body>","editable":false,"type":"text/html"},"subject":"Unable to access training Google Cloud Sales Credential","type":"update"},{"content":{"text":"<body><!-- [DocumentBodyStart:67e6b807-8421-4e61-a3c9-a08943f8a368] --><div class=\"jive-rendered-content\"><span>I am having issues with Capital Letter Confusion ie - Example.Com and example.com.I have major account confusion as a result.Thanks for reading.</span></div><!-- [DocumentBodyEnd:67e6b807-8421-4e61-a3c9-a08943f8a368] --></body>","editable":false,"type":"text/html"},"subject":"I am having issues with Capital Letter Confusion ie - Example.Com and example.com.I have major...","type":"update"},{"content":{"text":"<body><!-- [DocumentBodyStart:0013baf2-c12e-4869-9849-0959d99bdd77] --><div class=\"jive-rendered-content\"><p>or how can I know who is the admin? if they no longer have any IT administrator and the last one they had quits two years ago with zero documentation?</p></div><!-- [DocumentBodyEnd:0013baf2-c12e-4869-9849-0959d99bdd77] --></body>","editable":false,"type":"text/html"},"subject":"Why a gsuite edu user shows that his admin is a domain instead of an name or email account?","type":"discussion"},{"content":{"text":"<body><!-- [DocumentBodyStart:907a55ec-9332-43fe-876c-00384af7be79] --><div class=\"jive-rendered-content\"><span>Startup FB Data Science University will launch in the San Francisco, CA and Cloud Tech developers as individuals or companies can be Partner of the Startup Tech University without capital.</span><a class=\"jive-link-external-small\" href=\"https://www.cloudconnect.goog/external-link.jspa?url=xxx\" rel=\"nofollow\" target=\"_blank\">xxx</a></div><!-- [DocumentBodyEnd:907a55ec-9332-43fe-876c-00384af7be79] --></body>","editable":false,"type":"text/html"},"subject":"Startup FB Data Science University will launch in the San Francisco, CA and Cloud Tech developers...","type":"update"}.
    at [unknown function]([unknown file]:5:22)
    at [unknown function]([unknown file]:18:3)
    at doGet(Code:2:53)

有人以前看过这个吗?你能告诉我关于如何在此处进行的任何文档吗?

发生了什么事?

Page.html 文件中,您正在使用 include() 函数将数据插入 HTML 模板。

问题是数据本身(从 API 中获取)包含 HTML 标签,这些标签与您预先存在的 HTML 代码冲突,从而导致 "Malformed HTML content" 例外。

解决方案

为了解决这个问题,您应该对要插入到 HTML 模板中的数据使用 HTML character escaping。请参阅以下可能的解决方案:

Code.gs

function doGet(request) { 
  return HtmlService.createTemplateFromFile('Page').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function include(filename) { 
  var finalRequest = UrlFetchApp.fetch('https://www.cloudconnect.xxx....');

  var data = finalRequest.toString().replace("throw 'allowIllegalResourceCall is false.';", "").trim(); 

  data = JSON.parse(data);

  var returnData = [];

  for(var i in data){
    if(i == "list"){
     var content = data[i];
    }
    for(var i in content){
     var text = content[i];
      var json_string = JSON.stringify(text).replace(/[&]/g, "and");
      returnData.push(json_string);
    }
  }
  return escapeHtml(returnData);
}

var entityMap = {
  '&': '&amp;',
  '<': '&lt;',
  '>': '&gt;',
  '"': '&quot;',
  "'": '&#39;',
  '/': '&#x2F;',
  '`': '&#x60;',
  '=': '&#x3D;'
};

function escapeHtml(string) {
  return String(string).replace(/[&<>"'`=\/]/g, function (s) {
    return entityMap[s];
  });
}

修改

  • 添加了 escapeHtml 函数,可用于转义作为参数传递的字符串中存在的 HTML 个字符。
  • escapeHtml 函数使用的 entityMap 变量添加到 escape/translate 字符。
  • 修改了include()函数的return语句。现在它不再返回字符串本身,而是 returns 字符串的转义版本。