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

通过PHP隐藏路径加载GLB到Babylon.js时遇JSON解析错误求助

问题:Babylon.js加载GLB文件报错(JSON解析失败)

我想通过隐藏真实文件路径的方式,用Babylon.js加载3D GLB文件,编写了对应的HTML及PHP代码,但控制台出现以下错误:

Load Error: There was an error loading the model. Unable to load from https://example.com/view.php?id=blablabla.glb: importMesh of undefined from undefined version: undefined, exporter version: undefined importMesh has failed JSON parse

问题根源

PHP代码中设置了Content-Disposition: attachment,这会让浏览器将响应识别为需要下载的文件,而非可直接解析的资源。Babylon.js无法正确读取GLB二进制数据,进而触发JSON解析错误(GLB是二进制格式,错误提示是因为加载器读取到的内容不符合预期)。

修复方案

  • 移除attachment类型的Content-Disposition头,改用inline,让浏览器直接处理资源而非触发下载
  • 确保PHP文件开头无空白、BOM等多余输出,避免破坏GLB二进制数据完整性
  • 可选添加Content-Length头,明确文件大小,提升加载稳定性

修改后的代码

PHP(view.php)

<?php
if ($_GET['id'] !== "blablabla.glb") {
    exit();
}

$file = '3dmodel.glb';
if (file_exists($file)) {   
    header('Content-Type: model/gltf-binary');
    // 用inline允许浏览器直接读取资源
    header('Content-Disposition: inline; filename="' . basename($file) . '"');
    // 添加文件长度头
    header('Content-Length: ' . filesize($file));
    
    ob_clean();
    flush();
    readfile($file);
    exit; 
} 
?>

HTML代码(无需修改,确认路径正确即可)

<html>
  <head>
    <title>Babylon.js Viewer - Display a 3D model</title>
    <script src="https://cdn.babylonjs.com/viewer/babylon.viewer.js"></script>
  </head>
  <body>
    <babylon model="https://example.com/view.php?id=blablabla.glb" templates.main.params.fill-screen="true"></babylon>
  </body>
</html>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 02:40:25