Ecommerce Shopify WordPress Discussion

admin-ajax.php 400 (Bad Request) in wordpress [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers. Closed yesterday. Improve this question I am develop a custom script try to upload CSV file in the wordpress using the Ajax jquery.I am getting the error "admin-ajax.php 400 (Bad Request)". sometimes i faced such as error and i also fix it.But today's i got the different problem, I cant understand and fix this error. can you help me how to fix this error ? Ajax script let ajax_url = ajax_object.ajax_url; jQuery(document).ready(function(){ jQuery('#upload_button').on("click", function(e){ e.preventDefault(); var fileInput = document.getElementById('csvfile'); var file = fileInput.files[0]; if (file) { /*var formData = new FormData(); formData.append('csvfile', file);*/ var formData = new FormData(); formData.append('action', 'my_csv_upload_action'); formData.append('nonce', ajax_object.nonce); formData.append('csvfile', file); var reader = new FileReader(); reader.onload = function(e) { var contents =; // Split CSV content by lines var lines = contents.split(/\r\n|\n/); // Validate each email address var validEmails = []; var invalidEmails = []; for (var i = 0; i < lines.length; i++) { var email = lines[i].trim(); //console.log("Email>>>", email); if (isValidEmail(email)) { validEmails.push(email); } else { invalidEmails.push(email); } } if (invalidEmails.length > 0) { alert('Invalid email addresses found: ' + invalidEmails.join(', ')); } // Only proceed if there are valid email addresses if (validEmails.length > 0) { jQuery.ajax({ url: ajax_url, method: 'POST', data: formData, processData: false, contentType: false, success: function(data){ console.log('submit'); // Handle success }, error: function(xhr, status, error) { console.log(xhr.responseText); } }); } else { console.log('No valid email addresses found'); } }; reader.readAsText(file); } else { console.log('No file selected'); } }); // Function to validate email address function isValidEmail(email) { //return email !== ''; var email = email.replace(/\s/g, ''); var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (!filter.test(email)) { return false } return true } }); Rest of the code of custom plugin function enqueue_custom_scripts() { wp_enqueue_script('jquery'); wp_enqueue_script( 'custom-script', plugins_url('/js/custom-script.js' , __FILE__ ) , array('jquery'), time(), true); /*wp_localize_script('custom-script', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')) 'nonce' => wp_create_nonce('my_csv_upload_nonce'),);*/ wp_localize_script('custom-script', 'ajax_object', array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('my_csv_upload_nonce'), )); } add_action('admin_enqueue_scripts', 'enqueue_custom_scripts'); add_action('wp_ajax_handle_csv_import', 'handle_csv_import'); add_action('wp_ajax_nopriv_handle_csv_import', 'handle_csv_import'); function handle_csv_import() { // Verify nonce /*if ( !isset( $_POST['security'] ) || !wp_verify_nonce( $_POST['security'], 'ajax_nonce_action' ) ) { die( 'Permission check failed!' ); }*/ check_ajax_referer('my_csv_upload_nonce', 'nonce'); global $wpdb; $fileMimes = array( 'text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain' ); // Validate selected file is a CSV file or not if (!empty($_FILES['csvfile']['name']) && in_array($_FILES['csvfile']['type'], $fileMimes)) { // Open uploaded CSV file with read-only mode $csvFile = fopen($_FILES['csvfile']['tmp_name'], 'r'); // Skip the first line fgetcsv($csvFile); // Parse data from CSV file line by line while (($getData = fgetcsv($csvFile, 10000, ",")) !== FALSE) { // Get row data $email = $getData[0]; // If user already exists in the database with the same email $query = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}csvfile WHERE email = %s", $email); $check = $wpdb->get_results($query); /*var_dump($query); exit();*/ if ($check) { $wpdb->update( "{$wpdb->prefix}csvfile", array('email' => $email), array('email' => $email) ); } else { $wpdb->insert( "{$wpdb->prefix}csvfile", array('email' => $email) ); } } // Close opened CSV file fclose($csvFile); // Redirect to index page wp_redirect(admin_url().'admin.php?page=csv-upload-page'); exit; } else { echo "Please select a valid file"; } } // Hook to add the menu page add_action('admin_menu', 'csv_upload_menu_page'); // Add a menu page function csv_upload_menu_page() { add_menu_page( 'CSV File Upload Page', 'CSV Upload', 'manage_options', 'csv-upload-page', 'csv_upload_page_content' ); } // Callback function to display content on the menu page function csv_upload_page_content() { $ajax_nonce = wp_create_nonce( 'ajax_nonce_action' ); ?> <div class="wrap"> <h1><?php esc_html_e('CSV File Upload', 'orange-smtp'); ?></h1> <form id="csv-upload-form" action="" method="post" enctype="multipart/form-data"> <b><?php esc_html_e("CSV File Upload :", "orange-smtp"); ?></b> <input type="file" id="csvfile" name="csvfile" accept=".csv"> <p class="submit"> <input type="hidden" name="security" value="<?php echo esc_attr( $ajax_nonce ); ?>"> <input type="submit" name="upload_button" id="upload_button" class="button button-primary" value="<?php esc_html_e('Upload CSV', 'orange-smtp'); ?>"> </p> </form> <div id="upload-response"></div> <?php //echo admin_url().'admin.php?page=csv-upload-page'; ?> </div> <?php }
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.