javascript - 为什么控制台给出错误:无法导入空路径?
问题描述
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<div><img alt="no photo" src="" /></div>
<script>
const myImage = document.querySelector("img");
let myReq = new Request("903178.jpg");
fetch(myReq)
.then(response => {
console.log(response); //*no output in console*
return response.blob();
})
.then(response => {
let objectURL = URL.createObjectURL(response);
myImage.src = objectURL;
});
</script>
</body>
</html>
我已从同一文件夹中获取图像以在 html 页面上呈现,但错误显示在控制台中
错误:无法导入空路径 CodeSandBox 链接 - https://codesandbox.io/s/hardcore-river-re84i?file=/index.html
解决方案
根据您提供的代码和框代码,问题是您在图像中添加了一个空的 src="" 。要使代码正常工作,您需要做的就是不给它初始 src 或在其中放入一些东西,例如src="#"
<body>
<div><img alt="no photo" /></div>
<script>
const myImage = document.querySelector("img");
let myReq = new Request("903178.jpg");
fetch(myReq)
.then(response => {
return response.blob();
})
.then(response => {
let objectURL = URL.createObjectURL(response);
myImage.src = objectURL;
});
</script>
推荐阅读
- javascript - 获取最大值的子数组的数组键
- python - 无法从 S3 获取对象元数据。在 aws Rekognition 中检查对象键、区域和/或访问权限
- kubernetes - 连接到 Kubernetes 中的持久卷?
- r - 在 R Shiny App 中连接 UI 和 SERVER 端的对象
- ruby-on-rails - Rails 3 迁移现在在 Rails 5 中失败
- javascript - 网络推送通知点击不起作用
- ruby-on-rails - Ruby Grape:如何将哈希数组作为参数发送
- javascript - 将控制器变量值传递给AngularJS中的指令
- vba - Excel VBA:如何删除特定范围内的图像
- apache-nifi - 如何使用 nifi 解压缩 .7z 文件?