Support » Developing with WordPress » WP Ajax HTTP code 400

  • Resolved freewill7

    (@freewill7)


    Hi everyone, it’s my first post here.
    I’m getting into the wordPress’s world by writing a plugin.
    However, my plugin should use ajax but it doesn’t really work.
    So, here’s my main file’s content:

    add_action("wp_ajax_nopriv_lbuilder_select", "lbuilder_ajax");
    
    function lbuilder_ajax(){
    	$type = $_GET['type'];
    	echo $type;
    	die();
    }
    

    It’s pretty easy. Now the jQuery code:

    
    		var data = {
    			'action': 'lbuilder_select',
    			'type': 'snk'
    		};
    		console.log(lbuilder.ajaxurl);
    		$.get(lbuilder.ajaxurl, data, function(response){
    			$('#lbuilder *').remove();
    			$('#lbuilder').append(response);
    		});

    lbuilder.ajaxurl was passed by the wp_localize_script:

    	public function load_stuff(){
    			wp_register_script('lbuilder-script', plugin_dir_url( __FILE__).'assets/js/main.js', array('jquery'), NULL, false);
    			wp_register_style('lbuilder-style', plugin_dir_url(__FILE__).'assets/css/lbuilder-form.css');
    			$lbuilder = array(
    				'ajaxurl' => admin_url('admin-ajax.php')
    			);
    			wp_localize_script('lbuilder-script', 'lbuilder', $lbuilder);
    	}
    add_action('init', 'load_stuff');
     

    Now, what am I doing wrong? I hope you can help me.
    bye

    EDIT: I solved it: I was trying the script as registered user so I had to add add_action('wp_ajax_lbuilder_select');

    • This topic was modified 3 years, 9 months ago by freewill7.
    • This topic was modified 3 years, 9 months ago by freewill7.
    • This topic was modified 3 years, 9 months ago by bcworkz.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP Ajax HTTP code 400’ is closed to new replies.