I have a simple file upload ajax request towards admin-ajax.php.
var data = new FormData();
jQuery.each(jQuery('input[data-field=file]')[0].files, function(i, file) {
data.append('file-'+i, file);
});
$.ajax({
url : '/wp-admin/admin-ajax.php',
data: data,
cache: false,
contentType: false,
processData: false,
method: 'POST',
success: function(data){
console.log(data);
},
error: function() {
console.log("Error");
}
});
e.preventDefault();
Now, if I send the ajax request anywhere else than admin-ajax.php (e. g. /test.php), the data is passed normally.
The problem is, that admin-ajax.php will always return 0 because it has the following lines:
if ( empty( $_REQUEST['action'] ) )
wp_die( '0', 400 );
If I try to pass the data like this (with processData: false):
data: {
action: 'cas_contact_form',
data: data,
},
admin-ajax.php still dies with a 0.
If I remove processData: false, I will get an Uncaught TypeError: Illegal invocation error, and I won't be able even to send the data to admin-ajax.php.
So far, it seems like a catch22 problem to me. Am I missing something?
datakey inside the data object. But the receiving end likely expects the data fields and theactionkey on the same level. Dodata.append('action', 'cas_contact_form');instead after you added the input fields todata, that should work.