javascript - 我收到“主线程上的同步 XMLHttpRequest”错误,但我的代码按预期工作
问题描述
这是我的一段代码,用于确保给定 URL 中是否有文件。
如果有基于该文件 URL 的文件,我们console.log('We have context for the question');
将 console.log('No available context');
代码按预期工作,但控制台中出现错误:
speak('note');
function contextExist(url) {
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open('HEAD', url, false);
xhr.send();
return resolve(xhr.status==200);
});
}
// Speak Module
async function speak(type) {
switch (type) {
case 'note':
let resolved = await contextExist('https://round-arm-authority.000webhostapp.com/Ultimate%20Video%20Hack/videos/vid1.mp4');
if(resolved === true){
console.log('We have context for the question');
} else {
console.log('No available context');
}
break;
}
}
每次我在控制台中收到此错误时:
因为我的代码工作正常,我想知道这是否是一个严重的错误?我该如何解决这个错误?
更新:
speak('note');
function contextExist(url) {
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open('HEAD', url, true);
xhr.onload = function (e) {
return resolve(xhr.status==200)
};
xhr.onerror = function (e) {
return resolve(xhr.status==200)
};
xhr.send();
});
}
// Speak Module
async function speak(type) {
switch (type) {
case 'note':
let resolved = await contextExist('https://round-arm-authority.000webhostapp.com/Ultimate%20Video%20Hack/videos/vid1.mp4');
if(resolved === true){
console.log('We have context for the question');
} else {
console.log('No available context');
}
break;
}
}
解决方案
推荐阅读
- regex - 如果 a a1 匹配 B:B 中的任何单元格,则 C
- google-cloud-storage - Google Cloud API Gateway 端点如何从 Cloud Storage 文件传送数据?
- typescript - 我的项目有 tsx 和 ts 文件,但我想在测试文件中禁用 ts
- python - 使用余弦核的 SVM - 带有狗和猫图像的数据集
- java - 在 Java 中输入命令行参数后,如何创建代码以使 c^2 = a^2 + b^2 中的 c 成为最长边?
- c# - 如何使用 LIKE 关键字简化或重写 Cosmos 查询
- reporting-services - SSRS 报表查看器默认显示打印布局选项中的扩展数据
- angular - 如何将分页连接到两个组件之间的数据源
- python - 使用子进程 python 获取日期?
- java - gradle 错误标记:无法使用连接到 gradle 分发运行构建操作