• I’m writing a plugin that should use ajax.
    Here’s my part of the main file of the plugin:

    add_action( 'wp_enqueue_scripts', 'fhw_register_script' );
    
    function fhw_register_script() {
    	wp_deregister_script( 'jquery' );
    	add_action( 'wp_ajax_fhw_do_ajax_request', 'fhw_do_ajax_request' );
    	
    	wp_register_script( 'jquery', 'https://code.jquery.com/jquery-latest.min.js');
    	wp_register_script( 'fhw_formular', plugins_url( '/js/formular.js', __FILE__ ), array( 'jquery' ) );
    	
    	wp_enqueue_script( 'fhw_formular' );
    	
    	wp_localize_script( 'fhw_formular', 'ajax_object',
                array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
    	
    	wp_enqueue_script( 'jquery' );
    }

    So if I call the formular.js my dev-console says

    Filed to load resource: the server responded with a status of 400 (Bad Request)

    for the File admin-ajax.php. What to do?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Where’s the actual AJAX call? Or the code hooked to respond to it? What you’ve got here won’t request admin-ajax.php from the front-end on its own, so couldn’t be causing the error.

    Also, don’t load your own jQuery.

    And you don’t need that last call to enqueue it. It’s already happening as a dependency of your script.

    Thread Starter fhwebdesign

    (@fhwebdesign)

    Alright, this is my code how I am calling it

    jQuery(document).ready( function($) {
    	
    	$('input').bind("change", function(e) {
    		onSelect();
    	});
    	
    	
    });
    
    function onSelect() {
    	var data = {
    		'action': 'fhw_getTyp',
    		'typ' : this.value
    	};
    	jQuery.post(ajax_object.ajax_url, data, function(response) {
    		$(".ergebnis").html(response);
    	});
    };
    • This reply was modified 7 years, 1 month ago by fhwebdesign.
    Moderator bcworkz

    (@bcworkz)

    Move your add action call to ‘wp_ajax_fhw_do_ajax_request’ to outside the enqueue scripts callback. admin-ajax.php requests do not try to enqueue scripts. Your add action call can execute on plugin load, it does not need to be in a callback.

    Thread Starter fhwebdesign

    (@fhwebdesign)

    Move your add action call to ‘wp_ajax_fhw_do_ajax_request’ to outside the enqueue scripts callback. admin-ajax.php requests do not try to enqueue scripts. Your add action call can execute on plugin load, it does not need to be in a callback.

    As I understand, I should use this code like so

    add_action( 'wp_enqueue_scripts', 'fhw_register_script' );
    add_action( 'wp_ajax_fhw_do_ajax_request', 'fhw_do_ajax_request' );
    function fhw_register_script() {
    	wp_register_script( 'fhw_formular', plugins_url( '/js/formular.js', __FILE__ ), array( 'jquery' ) );
    	
    	wp_enqueue_script( 'fhw_formular' );
    	
    	wp_localize_script( 'fhw_formular', 'ajax_object',
                array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
    	wp_enqueue_script( 'fhw_formular' );
    }

    The 400 Bad Request error persists.

    Moderator bcworkz

    (@bcworkz)

    Yes, that was my intention. But I missed another important detail. Please change that one line to this:
    add_action( 'wp_ajax_fhw_getTyp', 'fhw_do_ajax_request' );
    The action tag has to correspond to the action data passed from jQuery, not your callback name.

    You probably know this, but it’s worth noting that one must be logged in for that action to work. For non-logged in users, the action tag would be ‘wp_ajax_no_priv_fhw_getTyp’, added separately.

    Thread Starter fhwebdesign

    (@fhwebdesign)

    Yes, that was my intention. But I missed another important detail. Please change that one line to this:
    add_action( ‘wp_ajax_fhw_getTyp’, ‘fhw_do_ajax_request’ );
    The action tag has to correspond to the action data passed from jQuery, not your callback name.

    You probably know this, but it’s worth noting that one must be logged in for that action to work. For non-logged in users, the action tag would be ‘wp_ajax_no_priv_fhw_getTyp’, added separately.

    Ok, I did it like this:

    add_action( 'wp_ajax_fhw_ladeMarke', 'fhw_do_ajax_request' );
    add_action( 'wp_ajax_no_priv_fhw_ladeMarke', 'fhw_do_ajax_request' );
    
    function fhw_register_script() {
    	wp_register_script( 'fhw_formular', plugins_url( '/js/formular.js', __FILE__ ), array( 'jquery' ) );
    	
    	wp_enqueue_script( 'fhw_formular' );
    	
    	wp_localize_script( 'fhw_formular', 'ajax_object',
                array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
    	wp_enqueue_script( 'fhw_formular' );
    }

    But that is also not working, still the same 400 Error.

    Moderator bcworkz

    (@bcworkz)

    I’m assuming the wp_enqueue_scripts add action still exists for fhw_register_script() somewhere. And apparently the action value from your jQuery is now fhw_ladeMarke instead of fhw_getTyp like in the snippet posted in your first reply to Jacob?

    Let’s be sure the Ajax callback is getting called. It should be if the above is true. Place something like error_log('Notice: fhw_do_ajax_request() was called'); as the first line in fhw_do_ajax_request(). Do a test, then verify the message appears in the error log. Whether it exists or not will tell us what direction to look for problems.

    Thread Starter fhwebdesign

    (@fhwebdesign)

    I’m assuming the wp_enqueue_scripts add action still exists for fhw_register_script() somewhere.

    The script above is the actual, whole plugin main file.

    And apparently the action value from your jQuery is now fhw_ladeMarke instead of fhw_getTyp like in the snippet posted in your first reply to Jacob?

    I just renamed that, but in all files.

    Let’s be sure the Ajax callback is getting called. It should be if the above is true. Place something like error_log(‘Notice: fhw_do_ajax_request() was called’); as the first line in fhw_do_ajax_request(). Do a test, then verify the message appears in the error log. Whether it exists or not will tell us what direction to look for problems.

    Ehm.. should I have a fhw_do_ajax_request function? What should it contain? As said, the script in the reply above is my whole file (without comments). Oh, there is a addaction more to enqueue fhw_register_script() too.

    Moderator bcworkz

    (@bcworkz)

    Yes! You need a fhw_do_ajax_request() function because you have add_action( 'wp_ajax_fhw_ladeMarke', 'fhw_do_ajax_request' );
    That is the function that handles your Ajax request server side. I don’t know what your Ajax call is supposed to accomplish on the server, but that function is key to making it happen.

    When you do

    jQuery.post(ajax_object.ajax_url, data, function(response) {
    		$(".ergebnis").html(response);
    });

    a request is POSTed to ajax_url, which is the admin-ajax.php file. That file grabs the action value (‘fhw_ladeMarke’) from the passed data array and appends it to “wp_ajax_” to arrive at the action tag to which you added ‘fhw_do_ajax_request’. The admin-ajax.php file then executes
    do_action( 'wp_ajax_fhw_ladeMarke', 'fhw_do_ajax_request' );
    which calls your fhw_do_ajax_request() function to handle the Ajax request. Note the initial do instead of add. It’s a major distinction.

    I hope that makes sense. Understanding it is essential to understanding how Ajax in WP works. You should consider the process until it truly makes sense to you. It might not resolve your immediate 400 problem, but it’s just as important for resolving similar issues in the future. If you need clarification on any point, just ask. I’ll do my best to help your understanding.

    Thread Starter fhwebdesign

    (@fhwebdesign)

    Okay, thank you for that good explanation! The mistake was that I forgot to include the ladeMarke.php into the plugins main file. Thanks for your help!

    Moderator bcworkz

    (@bcworkz)

    Hah! Those dependencies! I’m glad you tracked down the problem.

    Thank you! You saved my day!!!
    I had the same problem – new version of plugin was not included :)))))))))))

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Plugin development admin-ajax.php error 400?’ is closed to new replies.