1

I'm facing a CORS error when I'm trying to download video from different domain. I tried a lot to solve it but couldn't. Below is my js code.

getVideoFile = function () {
   var xhr = new XMLHttpRequest(),
   blob;
   xhr.open("GET",myVideo, true);
   xhr.responseType = "blob";
   xhr.addEventListener("load", function () {
   if (xhr.status === 200) {
    blob = xhr.response;
    putVideoInDb(blob);
    window.alert("Video file downloaded");
   }
   else {
    window.alert("Unable to download video");
   }
   }, false);
   xhr.send();
  };

  putVideoInDb = function (blob) {
   var transaction = db.transaction(["Videos"], "readwrite");
   var store = transaction.objectStore("Videos");
   var vid = {
     videoName:videoName,
     video:blob,
    }
   var request = store.add(vid);
   request.onerror = function(e) {
     console.log("Error",e.target.error.name);
    }
   request.onsuccess = function(e) {
     console.log("Done!!");
    }
  };
Nikhil
  • 665
  • 2
  • 11
  • 25

2 Answers2

1
xhr.setRequestHeader('Access-Control-Allow-Headers', '*');

Set this header value before sending your request. For more information you can read here JavaScript - XMLHttpRequest, Access-Control-Allow-Origin errors

Community
  • 1
  • 1
ManishKumar
  • 1,476
  • 2
  • 14
  • 22
0

Take a look at this question: CORS error on same domain?

Of course this is a scenario where the domain is the same, but however the logic's the same...

There's a lot of articles out there about this topic.

Community
  • 1
  • 1
Caius
  • 2,084
  • 6
  • 31
  • 47
  • I want to do it using only javascript and not AJAX jquery. It would be very helpful if you could provide the solution using javascript. – Nikhil Aug 02 '16 at 11:13