微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Fetch API 无法加载 CORS 请求的 URL 方案必须是“http”或“https”

如何解决Fetch API 无法加载 CORS 请求的 URL 方案必须是“http”或“https”

如果只在本地运行html文件对于 CORS 请求,Fetch API 无法将 URL 方案加载为“http”或“https”。)我收到如下错误代码:如何尝试在本地运行它而不会出错? >

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div>Teachable Machine Image Model</div>
        <button type="button" onclick="init()">Start</button>
        <button type="button" onclick="predict()">예측</button>
        <script
            class="jsbin"
            src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
        ></script>
        <div class="file-upload">
            <button
                class="file-upload-btn"
                type="button"
                onclick="$('.file-upload-input').trigger( 'click' )"
            >
                Add Image
            </button>

            <div class="image-upload-wrap">
                <input
                    class="file-upload-input"
                    type="file"
                    onchange="readURL(this);"
                    accept="image/*"
                />
                <div class="drag-text">
                    <h3>Drag and drop a file or select add Image</h3>
                </div>
            </div>
            <div class="file-upload-content">
                <img class="file-upload-image" id="face-image" src="#" alt="your image" />
                <div class="image-title-wrap">
                    <button type="button" onclick="removeUpload()" class="remove-image">
                        Remove <span class="image-title">Uploaded Image</span>
                    </button>
                </div>
            </div>
        </div>

        <div id="webcam-container"></div>
        <div id="label-container"></div>
        <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@0.8/dist/teachablemachine-image.min.js"></script>
        <script>
            function readURL(input) {
                if (input.files && input.files[0]) {
                    var reader = new FileReader();

                    reader.onload = function (e) {
                        $('.image-upload-wrap').hide();

                        $('.file-upload-image').attr('src',e.target.result);
                        $('.file-upload-content').show();

                        $('.image-title').html(input.files[0].name);
                    };

                    reader.readAsDataURL(input.files[0]);
                } else {
                    removeUpload();
                }
            }

            function removeUpload() {
                $('.file-upload-input').replaceWith($('.file-upload-input').clone());
                $('.file-upload-content').hide();
                $('.image-upload-wrap').show();
            }
            $('.image-upload-wrap').bind('dragover',function () {
                $('.image-upload-wrap').addClass('image-dropping');
            });
            $('.image-upload-wrap').bind('dragleave',function () {
                $('.image-upload-wrap').removeClass('image-dropping');
            });
        </script>
        <script type="text/javascript">
            // More API functions here:
            // https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/image

            const URL = './my_model/';
            let model,labelContainer,maxPredictions;

            async function init() {
                const modelURL = URL + 'model.json';
                const MetadataURL = URL + 'Metadata.json';
                model = await tmImage.load(modelURL,MetadataURL);
                maxPredictions = model.getTotalClasses();


                labelContainer = document.getElementById('label-container');
                for (let i = 0; i < maxPredictions; i++) {
                    // and class labels
                    labelContainer.appendChild(document.createElement('div'));
                }
            }
        </script>
    </body>
    <!-- copyright (c) 2021 by Aaron Vanston (https://codepen.io/aaronvanston/pen/yNYOXR)
Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files (the "Software"),to deal in the Software without restriction,including without limitation the rights to use,copy,modify,merge,publish,distribute,sublicense,and/or sell copies of the Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,fitness FOR A PARTIculaR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR copYRIGHT HOLDERS BE LIABLE FOR ANY CLaim,damAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -->
</html>

代码是(包括许可证。)

  1. 请告诉我在 localhost 上使用它的权限,而不是 http 或 https。
  2. 请告诉我这个权限如何使用 hdf5 制作模型

解决方法

最简单的方法是运行本地 Web 服务器。你有安装节点吗?现在几乎每个人都这样做,但如果没有,请按照此处的说明进行操作:https://nodejs.org/en/download/

然后创建一个 index.html 文件并粘贴上面的代码。打开终端,导航到包含索引文件的目录并键入 npx serve。这将获取 https://www.npmjs.com/package/serve 并运行一个小型网络服务器。

索引文件的 URL 将处理到您的剪贴板。打开浏览器,粘贴链接,您应该会看到该文件。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。