i have some problem with ajax call back function. I use ajax and vuejs together, after a make an ajax request in vue method, i would like to access the variable of vuejs but it not working. Here is my code
This is my vuejs function: in this function i tried to upload image to server with ajax
submitFile() {
let url = base_url+"/acp/attach/upload";
let formData = new FormData();
let mod_name = $("#vinpModName").val();
let mod_id = $("#vinpModId").val();
let file_title = $("#file_title").val();
//handle multi files upload
for( var i = 0; i < this.files.length; i++ ){
let file = this.files[i];
var imgData = '';
if ( i > 0 ) file_title = file_title + ' ' + i;
formData.append('images', file);
formData.append('mod_name', mod_name);
formData.append('mod_id', mod_id);
formData.append('title', file_title);
$.ajax({
url: url,
data: formData,
contentType: false,
processData: false,
enctype: 'multipart/form-data',
type: "POST",
success: function (data) {
var response = jQuery.parseJSON(data); console.log(this.myUploadData);
imgData = response.imgData;
if ( response.code == 1 ) {
SwalAlert.fire({
icon: 'error',
title: response.text,
})
} else {
SwalAlert.fire({
icon: 'success',
title: response.text,
})
}
},
error: function (jqXHR, textStatus, errorThrow) { //console.log(errorThrow);
SwalAlert.fire({
icon: 'error',
title: 'Có lỗi xảy ra khi upload! Vui lòng thử lại sau',
})
}
})
console.log(this.myUploadData);
//this.myUploadData.push(imgData);
}
},
in the function i log the vuejs variable 2 time, 1 worked and 1 does not work
here, the result when i tested in browser
Can anyone tell me what's wrong with my script? i want to access to the this.myUploadData and push the return object to this variable Can any one help me with this!
success: function (data) {tosuccess: data => {and movedthis.myUploadData.push(imgData);into the success callback and it's still not working?myUploadDatain the Vue instance data? Could you update your question to show?