Title: Plugin Accepting Ajax?
Last modified: December 10, 2021

---

# Plugin Accepting Ajax?

 *  Resolved [Kline](https://wordpress.org/support/users/klinelozada/)
 * (@klinelozada)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/)
 * Hello,
 * Thanks for this plugin, been using this for so long, however been trying to run
   this with ajax, and it seems it always return error 400. I would like to confirm
   if this can’t really run in ajax?
 * Thanks

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Plugin Author [SilkyPress](https://wordpress.org/support/users/diana_burduja/)
 * (@diana_burduja)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15150310)
 * Hello,
 * would you please be more specific about how exactly are you “trying to run this
   with ajax”? You can also share the custom code you’re trying to add with the 
   plugin and is related to “trying to run this with ajax”.
 *  Thread Starter [Kline](https://wordpress.org/support/users/klinelozada/)
 * (@klinelozada)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15158601)
 * Hello,
 * There’s a plugin called Code Snippets and I’m using this to enqueue ajax script
 *     ```
       add_action('wp_ajax_my_action_hook', 'my_function'); 
       add_action('wp_ajax_nopriv_my_ajax_hook', 'my_function'); 
   
       add_action( 'wp_enqueue_scripts', 'my_enqueue' );
       function my_enqueue() {
           $upload_dir = wp_upload_dir();
           wp_enqueue_script( 'ajax-script', $upload_dir['baseurl'] . '/custom-css-js/152152.js', array('jquery') );
           wp_localize_script( 'ajax-script', 'my_ajax_object',
                   array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
       }
   
       function my_function() {
       	$_degree = $_POST['degreetype'];
       	$_areainterest = $_POST['areainterest'];
       	$_programlocation = $_POST['programlocation'];
               $_data = json_encode(array("degree" => $_degree, "areainterest" => $_areainterest));
       	wp_send_json_success( $_data );
       	exit();
       }
       ```
   
 * On the other hand I’m using this plugin to run the script as well
 * File: /custom-css-js/152152.js
 *     ```
       jQuery(document).ready(function($){
       	$('.filter-option').change(function() {
       		$.ajax({
       			type: 'POST',
       			dataType: "JSON",
       			url: cu_ajax_object.ajax_url,
       			data: {
       				action: 'my_ajax_hook',
       				degreetype: $('#DegreeType').val(),
       				areainterest: $('#AreaInterest').val(),
       				programlocation: $('#ProgramLocations').val()
       			},
       			success: function(response) {
       				console.log(response.title);
       			}
       		});
       	});
       });
       ```
   
    -  This reply was modified 4 years, 6 months ago by [Kline](https://wordpress.org/support/users/klinelozada/).
    -  This reply was modified 4 years, 6 months ago by [Kline](https://wordpress.org/support/users/klinelozada/).
    -  This reply was modified 4 years, 6 months ago by [Kline](https://wordpress.org/support/users/klinelozada/).
    -  This reply was modified 4 years, 6 months ago by [Kline](https://wordpress.org/support/users/klinelozada/).
    -  This reply was modified 4 years, 6 months ago by [Kline](https://wordpress.org/support/users/klinelozada/).
 *  Plugin Author [SilkyPress](https://wordpress.org/support/users/diana_burduja/)
 * (@diana_burduja)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15158974)
 * On the first sight that code should work alright.
 * There is one small error that might break the code: the ‘ajax-script’ code is
   localized under the “my_ajax_object” variable, but the JS code tries to send 
   the POST data to the “cu_ajax_object.ajax_url” URL instead of “my_ajax_object.
   ajax_url”.
 *  Thread Starter [Kline](https://wordpress.org/support/users/klinelozada/)
 * (@klinelozada)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15159236)
 * I did actually changed that to the one you recommended, but the weird part was
   that it’s returning error 400. It’s calling for ‘admin-ajax.php’ but error 400.
 *  Plugin Author [SilkyPress](https://wordpress.org/support/users/diana_burduja/)
 * (@diana_burduja)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15159436)
 * Also, the hook name in the first “add_action” function call is named “wp_ajax_my_action_hook”
   instead of “wp_ajax_my_ajax_hook”.
 *  Thread Starter [Kline](https://wordpress.org/support/users/klinelozada/)
 * (@klinelozada)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15159515)
 * It’s the same. It still gets error 400, that’s why I’m kind of confused that 
   I have set everything correctly, but the json doesn’t pass on ajax.
 *  Plugin Author [SilkyPress](https://wordpress.org/support/users/diana_burduja/)
 * (@diana_burduja)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15159627)
 * The following works alright on my side:
 *     ```
       jQuery(document).ready(function($){
           $('.filter-option').change(function() {
             $.ajax({
               url: cu_ajax_object.ajax_url,
               type: 'POST',
               data: {
                   post_details : 'abc',
                   action: 'my_action_hook'
               },
               error: function(error) {
                       console.log('error', error);
               },
               success: function(response) {
                       console.log( 'success', response );
               }
           });
         });
       }); 
       ```
   
 * and
 *     ```
       add_action('wp_ajax_my_action_hook', 'my_function');
       add_action('wp_ajax_nopriv_my_action_hook', 'my_function');
   
       add_action( 'wp_enqueue_scripts', 'my_enqueue' );
       function my_enqueue() {
           $upload_dir = wp_upload_dir();
           wp_enqueue_script( 'ajax-script', $upload_dir['baseurl'] . '/custom-css-js/152152.js', array('jquery') );
           wp_localize_script( 'ajax-script', 'cu_ajax_object',
                   array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
       }
   
       function my_function() {
           wp_send_json( 'return from the AJAX' );
           wp_die();
       }
       ```
   
 * Please copy/paste the code snippets exactly and see if you get an answer from
   the server. Afterwards you can fill in the my_function() function and the “data”
   method of the AJAX object, so you can send the degreetype, areainterest, programlocation
   variables
 *  Thread Starter [Kline](https://wordpress.org/support/users/klinelozada/)
 * (@klinelozada)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15160748)
 * Weird. It’s still error 400 on my end. Are you using: Code Snippets plugin for
   embedding the php script on wp_ajax hook?
 * The payload is working correctly, but the preview/response returns 0. It seems
   not getting the value from the my_function().
 *  Plugin Author [SilkyPress](https://wordpress.org/support/users/diana_burduja/)
 * (@diana_burduja)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15161081)
 * Would you move the PHP snippet in the theme’s functions.php file and the JS simply
   added at the end of one of the theme’s JS files? If you’re still getting the 
   400 error, then it’s not because of the Code Snippets or the Simple Custom CSS
   and JS plugin, but it’s because of the actual code.
 * I cannot debug your code at the distance. And obviously it was harboring enough
   errors to begin with. Once you get the code working alright directly from the
   theme, then you can try moving it in the Code Snippets and the Simple Custom 
   CSS and JS plugins and see if you get additional issues there.
 * On my test website last snippets work alright. So the short answer to your initial
   questions is: yes, the plugin accepts AJAX. But only under the assumption that
   the JS/PHP code is correct.
 *  Thread Starter [Kline](https://wordpress.org/support/users/klinelozada/)
 * (@klinelozada)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15161218)
 * Already did, I reviewed the code and all seems to be correct. I transferred the
   php script on the functions.php and it works perfectly. But I can’t seem to understand
   why if transferred to a Code Snippet plugin, the code doesn’t seem to work.
 * Error 400 persistently shows up.
 *  Thread Starter [Kline](https://wordpress.org/support/users/klinelozada/)
 * (@klinelozada)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15161321)
 * Thank you so much for helping out, this is already resolved. The first codebase
   I shared was actually from a debugging, and I haven’t included all the code on
   that, just a context on how it should be working.
 * But managed to make it work from the code you shared. Also yes, it is working
   perfectly on ajax and code snippet. Thank you

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Plugin Accepting Ajax?’ is closed to new replies.

 * ![](https://ps.w.org/custom-css-js/assets/icon-128x128.png?rev=1303730)
 * [Simple Custom CSS and JS](https://wordpress.org/plugins/custom-css-js/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-css-js/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-css-js/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-css-js/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-css-js/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-css-js/reviews/)

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)

 * 11 replies
 * 2 participants
 * Last reply from: [Kline](https://wordpress.org/support/users/klinelozada/)
 * Last activity: [4 years, 5 months ago](https://wordpress.org/support/topic/plugin-accepting-ajax/#post-15161321)
 * Status: resolved