You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cloudflare Worker与Google Scripts数据同步异常:无法获取Bybit最新数据

Bybit→Cloudflare Worker→Google Scripts 数据获取异常排查方案

1. 禁用Cloudflare Worker缓存

Cloudflare Worker默认会缓存静态响应,若初始空数组被缓存,Google Scripts后续调用只会拿到缓存值,而非最新Bybit数据。在Worker返回响应时添加强制不缓存的头:

const response = new Response(JSON.stringify(bybitData), {
  headers: {
    'Content-Type': 'application/json',
    'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
    'Pragma': 'no-cache',
    'Expires': '0',
    'Surrogate-Control': 'no-store'
  }
});

2. 校验Google Scripts请求逻辑

  • 确保请求方法(GET/POST)与Worker处理逻辑完全匹配,比如Worker只处理POST时,GAS不能用GET请求。
  • 开启muteHttpExceptions并打印完整响应信息,排查隐藏错误:
const workerUrl = '你的Worker接口地址';
const res = UrlFetchApp.fetch(workerUrl, {
  method: 'GET', // 对应Worker的处理方法
  muteHttpExceptions: true
});
console.log('响应状态码:', res.getResponseCode());
console.log('原始响应内容:', res.getContentText());

状态码非200时,直接查看响应文本就能定位Worker返回的错误信息。

3. 配置正确的CORS头

Google Scripts的请求属于跨域请求,若Worker未配置CORS,会被安全策略拦截,导致返回空内容。在Worker中添加CORS处理:

const corsHeaders = {
  'Access-Control-Allow-Origin': 'https://script.google.com', // 限制为GAS域名,比*更安全
  'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
  'Access-Control-Allow-Headers': 'Content-Type'
};

// 处理OPTIONS预检请求
if (request.method === 'OPTIONS') {
  return new Response(null, { headers: corsHeaders });
}

// 最终响应携带CORS头
return new Response(JSON.stringify(bybitData), {
  headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});

4. 确认Worker数据返回逻辑

检查Worker代码中,是否在成功获取Bybit数据后实际返回了数据,而非仅在日志中打印。避免以下错误:

// 错误示例:只打印日志,未返回数据
const bybitData = await fetchBybitData();
console.log(bybitData); // 仅日志输出,无响应返回

必须确保有return new Response(...)的逻辑,将Bybit数据封装成JSON响应返回。

内容的提问来源于stack exchange,提问作者전태진

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 21:32:43