XMLHttpRequest.open没有足够的参数

时间:2017-05-05 14:08:40

标签: javascript ajax xmlhttprequest xmlhttprequest-level2

我正在使用这里描述的AJAX2

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest?redirectlocale=en-US&redirectslug=DOM/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress

监视ajax请求的进度,但是我收到以下错误“TypeError:没有足够的XMLHttpRequest.open参数。”

任何人都可以帮忙解决这个错误的原因吗?

我的代码是:

                var oReq = new XMLHttpRequest();
                oReq.addEventListener("progress", updateProgress);
                oReq.addEventListener("load", transferComplete);
                oReq.addEventListener("error", transferFailed);
                oReq.addEventListener("abort", transferCanceled);

                oReq.open();

                // ...

                // progress on transfers from the server to the client (downloads)
                function updateProgress (oEvent) {
                  if (oEvent.lengthComputable) {
                    var percentComplete = oEvent.loaded / oEvent.total;
                    console.log(percentComplete+ " percent completed.");
                    // ...
                  } else {
                      console.log(" Unable to compute progress information since the total size is unknown.");
                    // Unable to compute progress information since the total size is unknown
                  }
                }

                function transferComplete(evt) {
                  console.log("The transfer is complete.");
                }

                function transferFailed(evt) {
                  console.log("An error occurred while transferring the file.");
                }

                function transferCanceled(evt) {
                  console.log("The transfer has been canceled by the user.");
                }

此行发生Actuall错误

oReq.open();

2 个答案:

答案 0 :(得分:1)

该文档中的代码示例不完整,工作代码。它旨在向您展示如何将进度跟踪添加到现有的,有效的XMLHttpRequest代码。

您需要自己填补空白(或者更好的是:构建基于XHR的基本脚本,然后将进度跟踪添加到其中)。这包括替换:

oReq.open();

真实地呼叫open。见the documentation for open。您至少需要提供HTTP方法和URL(两个字符串参数)。

答案 1 :(得分:1)

open()方法有三个参数 open('get',url,true或false或null)