- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个异步音频上传脚本,可以完美运行,但有一个问题困扰着我。它唯一搞乱的是错误处理,因为错误是在 xhr 状态为 0 的情况下处理的,但在上传继续时似乎没有错误。
xhr.upload.addEventListener("progress", keepPage(file,xhr),false);
xhr.upload.addEventListener("progress", uploadProgress,false);
xhr.upload.addEventListener("load", uploadComplete, false);
xhr.upload.addEventListener("error", uploadFailed(xhr.status), false);
xhr.upload.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "http://" + document.domain + "/script.php", true);
xhr.send(fd);
var uploading = true;
uploaded = false;
xhr.onreadystatechange=function(){
if (xhr.readyState==4 && xhr.status==200) {
var response = xhr.responseText;
if (response == "success"){
var uploaded = true;
processSuccess();
}
else {
var obj = $.parseJSON(response); //parse error JSON array
errReport = obj.errReport
uploadFailed(errReport);
}
}
}
uploading_tools(file, xhr, uploaded);
脚本开始上传后,error
事件就会被触发。我已将 console.log(xhr.status)
放入 uploadFailed
函数中,并触发状态 0。
我知道跨域调用 ajax 时会给出 xhr 状态 0,但如您所见,情况并非如此。
如果我无法摆脱这个恼人的错误,我将不得不在错误处理中忽略 xhr 状态 0,这是我想避免做的事情。
编辑:
xhr.open("POST", "http://" + document.domain + "/script.php", true);
xhr.upload.addEventListener("progress", keepPage(file,xhr),false);
xhr.upload.addEventListener("progress", uploadProgress,false);
xhr.upload.addEventListener("load", uploadComplete, false);
xhr.upload.addEventListener("error", uploadFailed(xhr.status), false);
xhr.upload.addEventListener("abort", uploadCanceled, false);
xhr.send(fd);
var uploading = true;
uploaded = false;
xhr.onreadystatechange=function(){
if (xhr.readyState==4 && xhr.status==200) {
var response = xhr.responseText;
if (response == "success"){
var uploaded = true;
processSuccess();
}
else {
var obj = $.parseJSON(response); //parse error JSON array
errReport = obj.errReport
uploadFailed(errReport);
}
}
}
uploading_tools(file, xhr, uploaded);
最佳答案
在访问 XMLHttpRequest
的任何其他属性或调用任何其他方法之前,始终调用 open()
。否则可能会发生奇怪的行为。
编辑我刚刚看到了光,你没有收到错误!你有这一行:
xhr.upload.addEventListener("error", uploadFailed(xhr.status), false);
您没有指定 uploadFailed()
作为错误事件的事件处理程序。 您正在调用uploadFailed(xhr.status)
,因此您将函数的返回值指定为事件处理程序!当然,uploadFailed 肯定不会返回任何内容,因此最终您没有错误处理程序。但是,当然,当执行此行时,您的函数将被调用!由于对 xhr.send()
的调用尚未发生,因此 xhr.status
为 0
。
您必须将行更改为
xhr.upload.addEventListener("error", uploadFailed, false);
就像您拥有其他人一样:
xhr.upload.addEventListener("progress", uploadProgress,false);
xhr.upload.addEventListener("load", uploadComplete, false);
新编辑
针对您的评论,它不起作用,因为您正在做同样的事情,而不是我告诉您做的事情。让我们看看我能否向您解释一下。
假设您的 uploadFailed
函数如下所示:
function uploadFailed(error) {
console.log(error);
return true; //you won't have this line, but let's assume you do.
}
现在,您有以下代码:
xhr.upload.addEventListener('error', uploadFailed(errReport), false);
那么,这条线在做什么?正是这样:
var f = uploadFailed(errReport); //uploadFailed is being called, not assigned to f
xhr.upload.addEventListener('error', f, false); //f is not a function, but the value 'true'!
你明白了吗?当执行该行时,您将调用函数并分配结果,而不是分配函数本身。如果您的问题是如何将错误值传递给错误函数,您可以这样做:
function uploadFailed() {
console.log(this); //when you assign this function as an event handler,
//'this' will be the object which fired the event (xhr.upload)
}
您不需要将 xhr 对象传递给函数,但如果出于某种原因您需要将错误作为参数传递,那么您必须这样做:
xhr.upload.addEventListener('error', function() { uploadFailed(xhr.status); }, false);
通过这种方式,您可以定义一个新函数,该函数的作用域中包含您的 xhr
对象,并且可以将其传递给您的错误函数。
我希望您能够区分函数和函数调用之间的区别,因为这是必不可少的。
关于javascript - xhr 错误状态为 0,但脚本无论如何都会继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22491893/
我想制作一个主要功能取决于发送短信的应用程序。在我开发 android(native) 之前,但现在我使用 React-Native 使其适用于 IOS 和 Android。 在android中,如果
我想重新运行一个测试类,包括它的 @BeforeMethod当其中任何一个是 @Test失败。我已经实现了 TestNG 重试逻辑来重新运行失败的测试用例,但我想运行整个类。 最佳答案 可以这样做。
前几天我遇到了一个接线员,-),而我找到的一个名称“arrow application”通常链接到 the other kind of Arrow present in Haskell ,这似乎无关。
我希望能够在我的sql语句中获取记录的ID: $result = $db->query("select id,FQDN, ip_address, ....."); 但是,我不希望它使用标题显示在导出中
我刚刚在Windows XP中激活了主题(通常使用经典的Win9x外观进行工作),并且看到两个面板是纯黑色的。其他面板也可以(颜色= clBtnFace)。 这两个面板的共同点是它们的父面板。两者都直
我正在研究使用数据库存储到该数据库条目的生成的链接,该链接包含有关该数据库条目的更多信息。因此,您会看到一些数据库,然后单击该条目并打开一个新页面,其中包含有关该条目的更多信息。 我一直在寻找一种可以
这里有几篇与此主题相关的帖子,但我已经应用了与我读到的所有内容相关的内容: CSS .infoWindow { width: 90px; height: 90px; } 创建信息窗口并
我目前正在编写使用sqlite3的脚本。最近,由于我的代码因错误而提前退出,我遇到了另一个程序正在使用该数据库的问题。 遇到类似问题,通常使用: conn = sqlite3.connect(...)
我只想在Redis数据库中使用1个键值对。并且该值将每60秒减少1。可能吗? 最佳答案 一个有趣的问题:)是的,您可以花一些技巧。 众所周知,Redis TTL会随着时间自动降低。因此,您可以将TTL
我尝试了数十种不同的方式来播放来自YouTube或服务器的视频,而没有使其进入全屏模式。我知道这是有可能的,因为YouTube应用程序允许这种情况发生,但似乎无法弄清楚该怎么做。请指教! 最佳答案 看
是否有办法在jade文件或其他模板引擎中编写服务器端nodejs代码(如down代码)? (或没有模板引擎): extends layout block content p Welcome to
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
您好,我最近下载并打开了适用于everyplay android的unity软件包,想知道我是否还能抓取玩游戏的用户上传到youtube的视频的URL?看来,实际共享功能的大部分代码是外部的,我看不到
我正在尝试在 Google 表单的网址中传递查询参数,并将该表单提交给其他人来填写,因此我希望 onSubmit 事件检查该查询参数并获取它。 我的 onSubmit 事件有以下代码: functio
我有一个这样的功能,我想将单元格放在网格列的左侧和右侧,现在它只是一个一个地放置,对于左侧网格列,它应该是 ,对于正确的,可以是 ,有没有根据索引区别对待,如果是奇数则属于左Grid Column
我有以下索引模板 { "index_patterns": "notificationtiles*", "order": 1, "version": 1, "aliases": {
我有一个Docker容器,该容器将具有大量(大约100个)自定义设置。与将默认值全部编码到DockerFile中相比,我希望具有更大的灵活性。我注意到docker run命令支持此选项: --env-
我有一个带有指针的谷歌地图。我已经对其进行了设置,因此每当缩放发生变化或视口(viewport)发生变化时,它都会相应地加载一组新的指针: google.maps.event.addListener(
我有一个php容器,每次启动容器时都需要启动php-fpm。现在,由于php-fpm config文件中的配置错误,fpm无法启动,因此,容器无法启动。无论如何,我可以在没有php-fpm的情况下启动
我正在运行没有sudo访问权限的docker容器 docker run -it --user 739000:8500 blabla... 无论如何,我可以在没有sudo访问的情况下在此docker容器
我是一名优秀的程序员,十分优秀!