0

I'm sorry if this is a duplicate of another question but i have looked and not found one so here goes. If this is a duplicate, then please post a link to the original so I may take a look.

When I attempt to use XMLHttpRequest to transfer a file to my dropbox I get a XHR failed loading OPTIONS error. I am not using ajax which is why I believe this is not a duplicate of previously asked questions.

I copied the code here for my code: https://blogs.dropbox.com/developers/2016/03/how-formio-uses-dropbox-as-a-file-backend-for-javascript-apps/

but here's my code:

var xhr = new XMLHttpRequest();
var passedData = new Array("one", "two", "three")
var filename = "test";
var file = new File(passedData, filename);
xhr.open('POST', 'https://content.dropboxapi.com/2/files/upload');
xhr.setRequestHeader('Authorization', 'Bearer ' + 'AccessToken');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.setRequestHeader('Dropbox-API-Arg', JSON.stringify({
    path: '/' +  file.name,
    mode: 'overwrite',
    autorename: false,
    mute: false,
}));
xhr.send(file);

Full Error: VM639 framework.js:82 XHR failed loading: OPTIONS "https://content.dropboxapi.com/2/files/upload". upload @ VM639 framework.js:82 onclick @ View.html:675

I just realized I forgot to mention that this was working just fine until march 25th. Might be irrelevant but I wouldn't know.

The above works fine, but the problem is when I paste it into my main script. I'm calling it the same exact way, with a button, and sending the same file, but i get that message.

10
  • note that "AJAX" stands for "asynchronous JavaScript and XML", so using XMLHttpRequest is technically making an Ajax request. Can you add the full error so that we know why the XHR fails?
    – Kaddath
    Commented Apr 1, 2019 at 9:10
  • Oh sorry about that VM639 framework.js:82 XHR failed loading: OPTIONS "content.dropboxapi.com/2/files/upload". upload @ VM639 framework.js:82 onclick @ View.html:675
    – Zice Zhao
    Commented Apr 1, 2019 at 9:13
  • Mmh this isn't helping much, can you add this error hadling xhr.onload = function() { if (xhr.status !== 200) { var errorMessage = xhr.response || 'Unable to upload file'; console.error('Failed with status (' + xhr.status + ') - error: ' + errorMessage ); } }; (before xhr.open). And see what it outputs in console?
    – Kaddath
    Commented Apr 1, 2019 at 9:39
  • Thx for telling me about console.error() though, if it wasn't for you I probably never would've know about it. It's certainly something I'll be using a lot.
    – Zice Zhao
    Commented Apr 1, 2019 at 10:32
  • if it doesn't print anything, you can also try xhr.onerror = function(event) { console.error('Error with status (' + event.target.status + ')'); }, or look at the network tab in the developper console for meaningful infos about the error.
    – Kaddath
    Commented Apr 1, 2019 at 11:35

1 Answer 1

-1

The problem was that I was doing this at local host. Once I stuck it on a server, it worked.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.