Fetch API 无法加载 URL 方案对于 CORS 请求必须是“http”或“https”
Posted
技术标签:
【中文标题】Fetch API 无法加载 URL 方案对于 CORS 请求必须是“http”或“https”【英文标题】:Fetch API cannot load URL scheme must be "http" or "https" for CORS request 【发布时间】:2021-04-20 04:58:16 【问题描述】:如果只在本地运行 html 文件 (Fetch API 无法为 CORS 请求将 URL 方案加载为“http”或“https”。)我收到如下错误代码:如何尝试在本地运行而不出错? p>
<!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="#" />
<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>
代码是(包括许可证。)
-
请告诉我在 localhost 上使用它的权限,而不是 http 或 https。
请告诉我这个权限如何使用 hdf5 制作模型
【问题讨论】:
【参考方案1】:最简单的方法是运行本地 Web 服务器。你有安装节点吗?这些天几乎每个人都这样做,但如果没有,请按照此处的说明进行操作:https://nodejs.org/en/download/
然后创建一个index.html
文件并粘贴上面的代码。打开终端,导航到包含索引文件的目录并输入npx serve
。这将获取 https://www.npmjs.com/package/serve 并运行一个小型 Web 服务器。
索引文件的 URL 将对应到您的剪贴板。打开浏览器,粘贴链接,您应该会看到该文件。
【讨论】:
以上是关于Fetch API 无法加载 URL 方案对于 CORS 请求必须是“http”或“https”的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 Fetch API 从 localhost 加载 Deezer API 资源
Fetch API 无法加载 webpack://... 错误