0

我使用以下 HTML 文件来测试 Autodesk Forge 的 Headless Viewer。测试网址如下所示: http://localhost:8080/HeadlessViewer.html?token={{Bearer}}&urn={{base64URN}}

令牌有 scope=data:read,urn 是 base64 格式。

<html>
<head>
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no" />
</head>
<body>
    <div id="MyViewerDiv"></div>

    <!-- The Viewer JS -->
    <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/three.min.js?v=v2.10.*"></script>
    <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/viewer3D.js?v=v2.10.*"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

    <!-- Developer JS -->
    <script>
        function getParameterByName(name, url) {
            if (!url) url = window.location.href;
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        }

        function initViewer(token, urn) {
            var viewerApp;
            var options = {
                env: 'AutodeskProduction',
                accessToken: token
            };

            var documentId = atob(urn); // 'urn:<YOUR_URN_ID>';
            Autodesk.Viewing.Initializer(options, onInitialized);

            function onInitialized() {
                viewerApp = new Autodesk.Viewing.ViewingApplication('MyViewerDiv');
                viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.Viewer3D);
                viewerApp.loadDocument(documentId, onDocumentLoaded);
            }

            function onDocumentLoaded(lmvDoc) {
                var modelNodes = viewerApp.bubble.search(av.BubbleNode.MODEL_NODE); // 3D designs
                var sheetNodes = viewerApp.bubble.search(av.BubbleNode.SHEET_NODE); // 2D designs
                var allNodes = modelNodes.concat(sheetNodes);
                if (allNodes.length) {
                    viewerApp.selectItem(allNodes[0].data);
                    if (allNodes.length === 1) {
                        alert('This tutorial works best with documents with more than one viewable!');
                    }
                } else {
                    alert('There are no viewables for the provided URN!');
                }
            }
        }

        $(document).ready(function () {
            var url = window.location.href,
                token = getParameterByName('token', url),
                urn = getParameterByName('urn', url);
            initViewer(token, urn);
        });
    </script>
</body>
</html>

但是,它在异常 XMLHttpRequest.responseText 处停止。请看附图:错误图片

4

1 回答 1

0

我尝试了您的代码,只是替换了“accessToken:<>”和“var documentId = <>”并且工作正常。查看您的代码,我相信问题可能出在以下行:

var documentId = atob(urn); // 'urn:<YOUR_URN_ID>';

atob函数将解码字符串,这意味着它不会在 Base64 上。但是documentId应该是这样的:

var documentId = 'urn:c29tZSByYW5kb20gd29yZHMgaGVyZQ==';

请确保documentId格式正确。

最后,请注意查看器需要 URL 安全编码。考虑在服务器上编码(传输更安全)或在客户端进行编码,请参阅此答案

于 2016-10-04T12:04:53.163 回答