1

我正在尝试将 base64 数据发布到服务器,但我收到一个错误“Microsoft JScript 运行时错误:无法获取属性 'addEventListener' 的值:对象为空或未定义”下面我附上了错误屏幕截图任何帮助,谢谢。

            var imagefile = canvas.toDataURL("image/png");
            imagefile = imagefile.replace("data:image/png;base64,", "");
            var oXHR = new XMLHttpRequest();
            oXHR.upload.addEventListener('loadstart', uploadFinish, false);
            oXHR.upload.addEventListener('progress', uploadProgress, false);
            oXHR.upload.addEventListener('error', uploaderror, false);
            oXHR.open('POST',"http://demo.donelook.com/eyewearweb/levellens.aspx",true);
            oXHR.send(imagefile);

在此处输入图像描述

4

2 回答 2

1

有错误oXHR.upload.addEventListener,应该是oXHR.addEventListener。另一个问题是addEventListener并非所有浏览器都支持。如果您使用纯 javascript,则必须执行此类操作。

if (!oXHR.addEventListener) {
    oXHR.attachEvent("loadstart", uploadFinish);
}
else {
    oXHR.addEventListener("loadstart", uploadFinish, false);
}

您可以将其编写为单独的函数并重用它以确保浏览器兼容性。像这样:

function addEventListener(element,type, listener, useCapture){
     if (!element.addEventListener) {
        element.attachEvent(type, listener);
     }
     else {
        element.addEventListener(type, listener, useCapture);
     }
}

你的代码:

var imagefile = canvas.toDataURL("image/png");
 imagefile = imagefile.replace("data:image/png;base64,", "");
 var oXHR = new XMLHttpRequest();
 addEventListener(oXHR,'loadstart', uploadFinish, false);
 addEventListener(oXHR,'progress', uploadProgress, false);
 addEventListener(oXHR,'error', uploaderror, false);
 oXHR.open('POST',"http://demo.donelook.com/eyewearweb/levellens.aspx",true);
 oXHR.send(imagefile);
于 2013-06-29T05:33:35.647 回答
0

我想你想要:

oXHR.addEventListener('loadstart', uploadFinish, false);
于 2013-06-29T05:28:14.857 回答