javascript ajax関数を使用して画像データをサーバ(Djangoアプリ)に送信するにはどうすればいいですか?Ajaxを使用して画像を送信するには
以下は私のコードです。
// Get filename of image
jsondata = JSON.parse(data);
image_file_name = jsondata.fileurl;
// document.getElementById('previewimage').src = image_file;
// I can show the image.
b64_image = btoa(unescape(encodeURIComponent(image_file)));
var credentials = {
filename: image_file_name,
image: b64_image,
};
// Send ajax request to the server
$.ajax({
url: HOST_NAME + "user/api/file_uploader/",
type: 'GET',
dataType: 'json',
data: credentials,
timeout: 10000,
})
.done(function (data) {
// Get the result
jsondata = JSON.parse(data);
alert("File upload completed...");
})
// If false...
.fail(function (XMLHttpRequest, textStatus, errorThrown) {
console.log("Upload error");
})
コンテンツを送信する場合は、サーバーからデータを回復する場合にのみGETが有用であるため、GET要求ではなくPOST要求を使用する必要があります。 – Driblou
アドバイスありがとうございます。しかし、私がPUTメソッドを使っても、リクエストはサーバーAPIに到達できませんでした。 –
@KazzzStudio - Ehhrm、PUT!= GET – enhzflep