Not enough arguments to XMLHttpRequest.open

1.2k views Asked by At

I am using AJAX2 as described here

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

to monitor progress of ajax request, but i am receiving following error "TypeError: Not enough arguments to XMLHttpRequest.open."

Can anyone help why this error occuring?

My Code is:

                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 error is occuring on this line

oReq.open();
2

There are 2 answers

0
姜延涛 On BEST ANSWER

open() method has three parameters open('get' , url , true or false or null)

0
Quentin On

The code example in that document is not complete, working code. It is designed to show you how to add progress tracking to existing, working XMLHttpRequest code.

You need to fill in the gaps yourself (or better yet: Build a basic XHR based script and then add the progress tracking to it). That includes replacing:

oReq.open();

with a real call to open. See the documentation for open. At a minimum you need to provide an HTTP method and a URL (both string arguments).