An example of a Javascript request ("PUT") to API Yandex.Drive?

Trying to upload a file using JQuery.ajax, but to no avail.

var file = $('input[type="file"]').prop("files")[0];
var formData = new FormData();
formData.append('file-input', file);

$.ajax({
 url: 'https://webdav.yandex.ru:443',
 type: 'PUT',
 data: formData,
 path: 'images/',
 beforeSend: function (xhr) {
 xhr.setRequestHeader('Authorization', 'OAuth <token>');
 xhr.setRequestHeader('Expect', '100-continue');
 xhr.setRequestHeader('Content-Type', 'application/binary');
 xhr.setRequestHeader('Content-Length', file.size);
 xhr.setRequestHeader('Host', 'webdav.yandex.ru');
 xhr.setRequestHeader('Accept', '*/*');
},
 success: function(data) {
console.dir(data);
}
});</token>

The request is sent with the GET method, instead I need PUT
October 3rd 19 at 01:46
2 answers
October 3rd 19 at 01:48
Solution
If you want using ajax to send a file, you don't get to do it just sterilita form (which is what happens in your code).

// a handler is hung on a file input
document.getElementById('upload').addEventListener('change', function(e) {
 var file = this.files[0];
 var xhr = new XMLHttpRequest();
 xhr.file = file; // not necessary if you create scopes like this
 xhr.addEventListener('progress', function(e) {
 var done = e.position || e.loaded, total = e.totalSize || e.total;
 console.log('xhr progress:' + (Math.floor(done/total*1000)/10) + '%');
 }, false);
 if ( xhr.upload ) {
 xhr.upload.onprogress = function(e) {
 var done = e.position || e.loaded, total = e.totalSize || e.total;
 console.log('xhr.upload progress: '+ done + ' / '+ total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
};
}
 xhr.onreadystatechange = function(e) {
 if ( 4 == this.readyState ) {
 console.log(['xhr upload complete', e]);
}
};
 xhr.open('put', url, true);
xhr.setRequestHeader("Content-Type","multipart/form-data");
xhr.send(file);
 }, false);
And is it even possible? cross-domain queries, no? - hattie.Johnston80 commented on October 3rd 19 at 01:51
Yeah, maybe if the server's CORS policy is allowed. - Merlin commented on October 3rd 19 at 01:54
October 3rd 19 at 01:50
Try this:
type: "PUT" (double quotes)
in my example, and so is PUT - hattie.Johnston80 commented on October 3rd 19 at 01:53

Find more questions by tags AJAXYandexjQueryJavaScript