0

I use the following HTML file to test the Headless Viewer of Autodesk Forge. The test url will look like: http://localhost:8080/HeadlessViewer.html?token={{Bearer}}&urn={{base64URN}}

The token has scope=data:read, urn is base64 format.

<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>

However, it stops at the exception XMLHttpRequest.responseText. Please see the attached image: Error image

Khoa Ho
  • 87
  • 2
  • 13
  • can you retry on incognito/private browser window? that should avoid local cache. Also, unless you have a reason, consider use viewer3D.min.js (smaller) – Augusto Goncalves Oct 03 '16 at 16:57
  • I use Incognito and have the same error. I intended to use viewer3D.js to debug the Viewer code to see what happens with this XMLHttpRequest error. – Khoa Ho Oct 04 '16 at 04:38

1 Answers1

0

I tried your code just replacing the "accessToken: <>" and "var documentId = <>" and worked fine. Looking at your code, I believe the problem may be at the following line:

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

The atob function will decode the string, which means it will not be on Base64. But the documentId should be like:

var documentId = 'urn:c29tZSByYW5kb20gd29yZHMgaGVyZQ==';

Please make sure the documentId is properly formed.

Finally, note the Viewer requires URL Safe encoding. Consider encoding on server (safer to transmit) or doing it on client-side, see this answer.

Community
  • 1
  • 1
Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44
  • var documentId = 'urn:' + urn; I should not decode the string but to keep the Base64. That's why XMLHttpRequest fails to get the response content. Thank you. – Khoa Ho Oct 05 '16 at 02:38