Currently my project need me to send file via ajax. I has been search from online and follow the step. Here is the result that I following. It really work perfectly of send file to server site but have issue of return json. I have been try echo string,int and json. Only json not returning.
I also tried remove the contentType:false, it really able return the json but not passing the file to backend.
from.php
var country_code = $('#country_code').val();
var operator = $('#operator').val();
//var upload_file = $('#upload_file')[0].files[0].name;
var upload_file_info = $("#upload_file").prop('files')[0];
console.log($);
var frmData = new FormData();
var action = $("#frmImport").attr('action');
frmData.append('upload_file', upload_file_info);
frmData.append('operator', operator);
frmData.append('country_code', country_code);
$.ajax({
type: 'post',
data: frmData,
url: action,
dataType: 'json',
cache: false,
contentType: false,
processData: false,
timeout: 30000,
success:
function(data) {
if(data['status'] == 1)
{
var txt;
var r = confirm(data['message']);
if (r == true) {
location.href = '/operator/importoperatorinfo?country_code='+data['upload_info']['country_code']+'&operator_id='+data['upload_info']['operator_id']+'&filename='+data['upload_info']['new_filename'];
} else {
return false;
}
}
else
{
alert(data['message']);
$('#upload_file').focus();
return false;
}
},
error:
function(error,data)
{
console.log(data);
}
});
process.php
<?php
echo json_encode(array('status'=>200));
?>