Reputation: 707
How I can send file with PUT method if I have text string? For example:
var new_data = 'hello world';
var formData = new FormData();
formData.append("thefile", new_data);
var new_req = new XMLHttpRequest();
new_req.open("PUT", p.href, false);
new_req.setRequestHeader("Content-Type", "multipart/form-data");
new_req.send(formData);
I getting empty response with HTML error 500 back. What I'm doing wrong? Uploader URL is right, I checked that.
Upvotes: 0
Views: 143
Reputation: 9724
Try add in your manifest.json
:
"permissions": [
"http://*.disk.yandex.net/"
]
Read about: https://developer.chrome.com/extensions/xhr
Read this in Yandex API
The only exception is for Chromium-based browser extensions:
Origin headers
that begin with the schemachrome-extension://
are simply ignored.
So the only thing missing was set permissions in your manifest.json.
Note: PUT is only part of the instruction in "verb" of the HTTP protocol, it alone nothing. This is what sets the SERVER-SIDE language.
PUT
and POST
are similar. POST
requires the times (depends on how you will send the file, eg. RAW
):
.overrideMimeType("multipart/form-data");
.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
In others words, try this:
xmlhttp.open("PUT", p.href, false);
if (xmlhttp && xmlhttp.overrideMimeType) {
xmlhttp.overrideMimeType("multipart/form-data");
}
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(formData);
Note: Prefer use Async, like this:
xmlhttp.open("PUT", p.href, true);//Async requires "true"
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log("OK", xmlhttp.responseText);
} else if (xmlhttp.readyState == 4) {
console.log("ERROR", xmlhttp.status);
}
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(formData);
Note: Async xmlhttp.open("PUT", p.href, true);
Note: Sync xmlhttp.open("PUT", p.href, false);
Error 500 can be a problem in htaccess (if apache) or other config in your server.
Upvotes: 1