Reputation: 1859
I have a trouble in my ajax call. When I tried to make a request from my PHP, the ajax returns in the error function and I don't know why because everything is okay. All of my data in my PHP are inserted correctly and I returned something like this in my PHP : echo json_encode($response);
and when I checked the browser network then navigate my ajax request, it gives me the success but in my ajax callback success function, it doesn't execute instead it goes to error.
Here is my ajax request:
let fileInput = $('#file_input')[0];
let postData = new FormData();
$.each(fileInput.files, function(k,file){
postData.append('requirement_files[]', file);
});
let other_data = $('#new_foster_applicant_frm_id').serializeArray();
$.each(other_data,function(key,input){
postData.append(input.name,input.value);
});
$.ajax({
url : baseurl+'user/post/new_foster_applicant',
type : 'POST',
data : postData,
contentType: false,
processData: false,
success : function(response) {
if(response.success === 'true') {
console.log('true');
// $('.returen_savs').html('Please copy the transaction code serve as your transaction receipt...');
// $('.error_hadble').html('<div class="alert alert-success fade in m-b-15">'+'Transaction Code:'+response.transaction_code+'</div>');
}else {
console.log('false');
// $('.returen_savs').html('');
// $('.error_hadble').html('<div class="alert alert-danger fade in m-b-15">'+'Error in submitting. Please try again later'+'</div>');
}
},
error : function(e) {
console.log(e);
}
});
Upvotes: 1
Views: 99
Reputation: 8103
Assuming that the data you returned is really JSON type.
Please
(1) remove contentType:false and processData:false statements
(2) use method instead of type for 'POST' (assuming you are using new version)
(3) add JSON.parse to parse the JSON data returned
Hence , change
$.ajax({
url : baseurl+'user/post/new_foster_applicant',
type : 'POST',
data : postData,
contentType: false,
processData: false,
success : function(response) {
if(response.success === 'true') {
to
$.ajax({
url : baseurl+'user/post/new_foster_applicant',
method : 'POST',
data : postData,
// contentType: false,
// processData: false,
success : function(response) {
var obj = JSON.parse(response);
if(obj.success === 'true') {
Please also make sure that postData is not null in your case.
Upvotes: 1