Refer: https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload https://developer.mozilla.org/en-US/docs/Web/API/FormData/append https://developer.mozilla.org/en-US/docs/Web/API/File
Got: method: 'POST', type: 'POST', // For jQuery < 1.9
<input id="avatar-upload" type="file" accept="image/*">
$('#avatar-uoload').on('change', function() {
/* $() get arrays | files also is an array */
var img = $(this)[0].files[0];
var formData = new FormData();
/* name, value [, filename] */
formData.append('avatar', img, 'avatar' +
img.type.replace(/image\\/i, '');
);
formData.append('upload_file', true);
$.ajax({
method: 'POST',
dataType: 'JSON',
url: _URL.upload,
data: formData,
async: true,
cache: false,
contentType: false,
processData: false,
success:function(data){
console.log("success");
console.log(data);
},
error: function(data){
console.log("error");
console.log(data);
}
});
});