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

使用BigCommerce API POST脚本遇400错误,求解决方案

问题

尝试通过BigCommerce API POST一段脚本到页面head中,请求体如下:

{  
  "name": "string",  
  "description": "string",  
  "html": "string",  
  "src": "(function (sCDN, sCDNProject, sCDNWorkspace, sCDNVers) {    if (      window.localStorage !== null &&      typeof window.localStorage === 'object' &&      typeof window.localStorage.getItem === 'function' &&      window.sessionStorage !== null &&      typeof window.sessionStorage === 'object' &&      typeof window.sessionStorage.getItem === 'function'    ) {      sCDNVers =        window.sessionStorage.getItem('wx_preview_version') ||        window.localStorage.getItem('wx_preview_version') ||        sCDNVers;    }    window.x= window.x|| {};    window.x.config = window.x.config || {};    window.x.config.frontend = window.x.config.frontend || {};    window.x.config.frontend.cdnhost =      sCDN + '/get/' + sCDNWorkspace + '/web/' + sCDNVers + '/';    window.x.config.frontend.vers = sCDNVers;    window.x.config.frontend.env = sCDNWorkspace;    window.x.config.frontend.project = sCDNProject;    window._wx = window._wx || [];    var f = document.getElementsByTagName('script')[0];    var j = document.createElement('script');    j.async = true;    j.src = window.x.config.frontend.cdnhost + 'sss.js';    f.parentNode.insertBefore(j, f);  })(    'https://xxxx.x-x-x.mybigcommerce.com',    'x-test-env',    'live',    '_'  );",  
  "auto_uninstall": true,  
  "load_method": "default",  
  "location": "head",  
  "visibility": "storefront",  
  "kind": "src",  
  "api_client_id": "string",  
  "consent_category": "essential",  
  "enabled": true,  
  "channel_id": 1
}

请求后返回400错误:

{"status": 400,"title": "Input is invalid","type": "https://developer.bigcommerce.com/api-docs/getting-started/api-status-codes","errors": {} }

怀疑问题出在脚本中的大括号{}上,请问如何成功将该脚本POST到页面head中?

解决方法

  • 修正kind字段与内容的匹配:BigCommerce脚本API中,kind设为src时,src字段应填写脚本的外部URL,而非内联脚本内容。如果要上传内联脚本,需将kind改为script,并把脚本内容移到html字段,同时清空src字段。调整后的核心请求体示例:
    {
      "name": "自定义配置脚本",
      "description": "加载CDN资源的配置脚本",
      "html": "(function (sCDN, sCDNProject, sCDNWorkspace, sCDNVers) {    if (      window.localStorage !== null &&      typeof window.localStorage === 'object' &&      typeof window.localStorage.getItem === 'function' &&      window.sessionStorage !== null &&      typeof window.sessionStorage === 'object' &&      typeof window.sessionStorage.getItem === 'function'    ) {      sCDNVers =        window.sessionStorage.getItem('wx_preview_version') ||        window.localStorage.getItem('wx_preview_version') ||        sCDNVers;    }    window.x= window.x|| {};    window.x.config = window.x.config || {};    window.x.config.frontend = window.x.config.frontend || {};    window.x.config.frontend.cdnhost =      sCDN + '/get/' + sCDNWorkspace + '/web/' + sCDNVers + '/';    window.x.config.frontend.vers = sCDNVers;    window.x.config.frontend.env = sCDNWorkspace;    window.x.config.frontend.project = sCDNProject;    window._wx = window._wx || [];    var f = document.getElementsByTagName('script')[0];    var j = document.createElement('script');    j.async = true;    j.src = window.x.config.frontend.cdnhost + 'sss.js';    f.parentNode.insertBefore(j, f);  })(    'https://xxxx.x-x-x.mybigcommerce.com',    'x-test-env',    'live',    '_'  );",
      "src": "",
      "kind": "script",
      "auto_uninstall": true,
      "load_method": "default",
      "location": "head",
      "visibility": "storefront",
      "api_client_id": "你的实际API客户端ID",
      "consent_category": "essential",
      "enabled": true,
      "channel_id": 1
    }
    
  • 清理占位符字段:将api_client_id的"string"替换为你的API客户端真实ID,name和description也建议填写有实际意义的内容,避免使用占位符。
  • 验证JSON格式合法性:使用JSON校验工具检查整个请求体,确保没有语法错误(比如未转义的特殊字符、逗号遗漏、引号不匹配等)。虽然你的脚本用了单引号,不会和JSON的双引号冲突,但如果脚本中出现双引号,需要转义为\"。

内容的提问来源于stack exchange,提问作者SKS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 23:06:11