UrlFetchApp.fetch() 给出语法错误:意外的标记 '<', "<!DOCTYPE "... is not valid JSON about 1/5 of the time Google script

问题描述 投票:0回答:1

我已经使用

UrlFetchApp.fetch()
很多年了,没有任何问题

但是最近,我大约每刷新五次就会随机收到此错误消息。

语法错误:意外的标记'<', "

我花了几个小时研究如何解决这个问题,但没有发现任何有效的方法。

任何见解都值得赞赏。

function fetch() {
  var source = UrlFetchApp.fetch("https://sum-app.net/projects/151546202312131392/download_data/kumu_json").getContentText();
  var data = JSON.parse(source);
  return data;
}
google-apps-script google-sheets urlfetch
1个回答
0
投票

这意味着您的服务器正在发送 Html 而不是 json。发生这种情况的原因有多种,例如速率限制(在这种情况下,您可以尝试指数退避算法)或内部服务器错误。

function fetch() {
  const res = UrlFetchApp.fetch("https://sum-app.net/projects/151546202312131392/download_data/kumu_json"),
    source = res.getContentText(),
    code = res.getResponseCode();
  if(code === 200){
     var data = JSON.parse(source);
     return data;
  } else {
     console.error({code})
     console.error(res.getContentText())
     throw new Error("Oops! Something went wrong!")
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.