【问题标题】:Not enough arguments to XMLHttpRequest.openXMLHttpRequest.open 的参数不足
【发布时间】:2017-10-04 01:32:25
【问题描述】:

我正在使用此处描述的 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: Not enough arguments to 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.");
                }

此行发生实际错误

oReq.open();

【问题讨论】:

  • 代码在哪里?

标签: javascript ajax xmlhttprequest xmlhttprequest-level2


【解决方案1】:

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

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

oReq.open();

真正致电open。见the documentation for open。您至少需要提供一个 HTTP 方法和一个 URL(两个字符串参数)。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-02-25
      • 2017-01-19
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多