Request to IP camera Javascript

  javascript

i’m trying to make a request to an IP camera, but the camera needs Basic authentication, i need to get this request and show in an Iframe or img, but i don’t know how to get that, i see how the request to the camera stays always waiting to finish an send some data, but i don’t know how to get it and pass to the Iframe, thank’s for any help.

Code Example:

function makeRequest(method, url, data) {
    return new Promise(function (resolve, reject) {
      var xhr = new XMLHttpRequest();

      xhr.open(method, url);
      xhr.setRequestHeader(
        "Authorization",
        "Basic " + btoa("user:password")
      );
      xhr.onload = function () {
        if (this.status >= 200 && this.status < 300) {
          resolve(xhr.response);
        } else {
          reject({
            status: this.status,
            statusText: xhr.statusText,
          });
        }
      };
      xhr.onerror = function () {
        reject({
          status: this.status,
          statusText: xhr.statusText,
        });
      };
      if (method == "POST" && data) {
        xhr.send(data);
      } else {
        xhr.send();
      }
    });
  }

  //GET example
  makeRequest("GET", "http://ip_camera_url").then(
    function (data) {
      var results = JSON.parse(data);
      console.log(results);//Never come here
    }
  );

Source: Ask Javascript Questions

LEAVE A COMMENT