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
-
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”.
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); } }); }); });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”.
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.
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”.
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.
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
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().
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.
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.
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
The topic ‘Plugin Accepting Ajax?’ is closed to new replies.