Simple AJAX call to upload a file.
-
I’m trying to do something fairly simple (or so i THOUGHT), with the problem being that I can’t isolate what the actual error is. Very simply, i have created a plugin which displays a form where i am soliciting the user to upload a csv file. Upon submitting the form an ajax call assembles everything and calls a function which simply displays the contents in an HTML table (so far). The problem is that i get a “POST 404 (not found)”. I can’t figure out what i am missing here, i’ve watched some tutorials, followed the examples, etc…..
<div id="file-upload-form" style="display: none;">
<form id="csv-form" enctype="multipart/form-data">
<label for="csv_file">CSV File Name:</label>
<input type="file" id="csv_file" name="csv_file" accept=".csv">
<button type="button" id="upload-csv">Upload</button>
</form>
</div>
<div id="csv-table"></div>My AJAX call:
$('#upload-csv').on('click', function () {
alert('Doing the right thing');
const fileInput = $('#csv_file')[0].files[0];
if (!fileInput) {
alert('Please select a CSV file.');
return;
}
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
const formData = new FormData();
formData.append('csv_file', fileInput);
formData.append('action', 'process_csv');
alert(formData);
$.ajax({
//url: ajax_new_obj.ajax_url,
url: ajaxurl,
type: 'post',
data: formData,
processData: false,
contentType: false,
success: function (response) {
if (response.success) {
$('#csv-table').html(response.data);
} else {
alert(response.data);
}
},
error: function (data, status) {
console.log(data, status);
alert('An error occurred while loading this payroll file. Please check the file format and try again.');
}
});
});
});And finally, my receiving function. I do know that many times you can get the ‘Bad Request’ error if you don’t add the appropriate actions, which i have added here. This is my code handler, which never executes due to the POST 404 Not Found error.
// Handle AJAX request to process CSV
add_action('wp_ajax_process_csv_file', 'process_csv_file');
add_action('wp_ajax_nopriv_process_csv_file', 'process_csv_file');
function process_csv_file() {
if ( ! wp_verify_nonce( $_POST['nonce'], 'ajax-nonce' ) ) {
die ( 'You do not have permission to access this routine - nonce violation.'); // Die if invalid Nonce
}
if (!empty($_FILES['csv_file']['tmp_name'])) {
$file = $_FILES['csv_file']['tmp_name'];
$csv_data = array_map('str_getcsv', file($file));
$html = '<table border="1">';
$html .= '<tr><th>Customer Name</th><th>Date</th></tr>';
foreach ($csv_data as $index => $row) {
if ($index === 0) continue; // Skip header row
$html .= '<tr><td>' . esc_html($row[0]) . '</td><td>' . esc_html($row[1]) . '</td></tr>';
}
$html .= '</table>';
wp_send_json_success($html);
} else {
wp_send_json_error('No file uploaded.');
}
}I had originally set up the ajax url in my plugin’s root file like this:
function load_admin_assets() {
wp_enqueue_script('ajax-new', plugins_url('/js/ajax-new.js', __FILE__), array('jquery'), null, true);
wp_localize_script('ajax-new', 'ajax_new_obj', array('ajax_url' => admin_url('admin-ajax.php'),
'baseUrl' => esc_url( home_url() ),
'nonce' => wp_create_nonce('ajax-nonce')));
}
add_action('admin_enqueue_scripts', 'load_admin_assets');That didn’t work either, so i went to the above approach of hard-coding the ajax url into the code, no love was returned. I’ve swapped in/out most every argument, including nonce that i can think to try and still, no love was returned. I’ve also removed the nonce references completely, thinking that might be the root of it. No such luck…. Can anyone help with some knowledge as far as what I can do to track down the source of the problem? The ‘404 Not Found’ error is VERY vague and i lack some knowledge in how to dig deeper. Thanks and cheers!
- You must be logged in to reply to this topic.