Ajax bad request 400
-
hi I’m trying to send with the POST method some data to a notification page.
I created a javascript file an ajax php handler page a the notification page.
here below you find the code I wrote in php for enqueuing the scripts etc.
wp_register_script('user_notifications_js', get_theme_file_uri('/js/notification.js') , array('jquery') , '1.0.1' , true ); if ( is_page('notifications') ){ // scripts wp_localize_script( 'user_notifications_js', 'user_notifications_object_js', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); wp_enqueue_script('user_notifications_js'); }
the javascript function I’m using is:
let ntfId = ntf.getAttribute('id'); jQuery(document).ready(function($) { console.log(user_notifications_object_js.ajaxurl); $.ajax({ url: user_notifications_object_js.ajaxurl, method: 'POST', data: { id : ntfId, status : 'read', action : 'rm_ntfs_handler', }, error: function( response ) { console.log(response.statusText); }, succes: function( response ) { console.log(response.statusText); }, }); }); }
Now here is the trouble I’ve seen many tutorials online
and they all use a new page to handle the ajax request, so i prepared a new page correctly included in the function.php.inside this page I wrote this code:
add_action( 'wp_ajax_rm_ntfs_handler', 'rm_ntfs_handler' ); add_action( 'wp_ajax_nopriv_rm_ntfs_handler', 'rm_ntfs_handler' ); function rm_ntfs_handler(){ echo 'inside'; }
and nothing happen not even an error it seems like that the xhr request doesn’t exists, so I moved the above mentioned code (the last part) in the notification page and here at least I get the 400 error bad request.
what am i doing wrong? any suggestion?
any help will be very appreciated thank you in advance.
- The topic ‘Ajax bad request 400’ is closed to new replies.