0

I am being write a program using javascript and cordova plugin, otherwise some of download and upload methods using cordova FileTransfer.

But unfortunately when download or upload any file, It always show :

"Access to XMLHttpRequest at 'http://doc.google.com/www/data/survey/test.txt' from origin 'http://localhost:007/index.html#home' has been block by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Here is the download script:

let fileTransfer = new FileTransfer(), store = cordova.file.dataDirectoy;

fileTransfer.download("http://doc.google.com/www/data/survey/test.txt", store + "test.txt",
  function(entry) { ... },
  function(error) { console.log(error); },
  false,
  {
      headers: {
          "Access-Control-Allow-Origin": "*",
          "Content-Type": "text/plain",
          "Authorization": "Basic dGVzdHVzZXJuYW1lOnR1c3RwYXNzd29yZA=="
      }
  }
);

Anybody could help pls?

How to tell browser for "CORS permit" using cordova FileTransfer?

Thanks in advance.

OO7
  • 660
  • 4
  • 10
  • To the right of this question is a Related list of questions, all related to CORS. Most of the answers note that the Access-Control-Allow-Origin header must be sent by the server responding to the request, not by the client making the request. – Heretic Monkey Feb 25 '20 at 22:00
  • I agree with you, but I cant set the server rules. And for the local test server with appropriates access control response header, the error always show up. – OO7 Feb 25 '20 at 22:05
  • Does this answer your question? [How does Access-Control-Allow-Origin header work?](https://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work) – Heretic Monkey Feb 25 '20 at 22:07
  • Its no sound cordova FileTransfer plugin. And on the browser status code: 200 (OK). – OO7 Feb 25 '20 at 22:42

1 Answers1

0

add https://cors-anywhere.herokuapp.com/ in your URL and it will bypass CORS error.

In your case you can write like this:

let fileTransfer = new FileTransfer(), store = cordova.file.dataDirectoy;

fileTransfer.download("https://cors-anywhere.herokuapp.com/http://doc.google.com/www/data/survey/test.txt", store + "test.txt",
  function(entry) { ... },
  function(error) { console.log(error); },
  false,
  {
      headers: {
          "Access-Control-Allow-Origin": "*",
          "Content-Type": "text/plain",
          "Authorization": "Basic dGVzdHVzZXJuYW1lOnR1c3RwYXNzd29yZA=="
      }
  }
);
Gaurab Kumar
  • 2,144
  • 2
  • 17
  • 29
  • Thanks a lot. However I think that is no safe, associates to contents grabbing by 3rd party. Anyway that is a good idea to generate crawler script that run on internal server. – OO7 Feb 26 '20 at 15:15