• Hi, since the update 4.9.4 I receive an admin-ajax.php 400 error when trying to load the project.

    The ajaxurl is loaded correctly.
    Here are the snippets:
    function add_js_scripts() {
    wp_enqueue_script( ‘script’, get_template_directory_uri() . ‘/js/ajax-navigation.js’, array(‘jquery’), ‘1.0’, true );

    // pass Ajax Url to script.js
    wp_localize_script(‘script’, ‘ajaxurl’, admin_url( ‘admin-ajax.php’ ) );
    }
    add_action(‘wp_enqueue_scripts’, ‘add_js_scripts’);

    $.ajax({

    url: ajaxurl,
    beforeSend: function() {

    Can you help? I’ve tried disabling plugins but no luck

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Have you added a hook into the wp_ajax_ hooks, and defined an action in the AJAX data?

    Thread Starter cmdcmdcmd

    (@cmdcmdcmd)

    Hi, no sure what you mean here as I am loading the content though the post ID and using the .load() function

    $(document).on( ‘touchstart click’, ‘.portfolio_item’, function( ) {
    $port_holder = $(‘.portfolio_holder’);
    $video_holder = $(‘#video_holder’);
    $currentitem = $(this);

    post_id = $(this).attr(‘data-id’);
    post_link = $(this).attr(‘data-id’),
    thetitle = $(this).attr(‘title’),
    newcontent = post_link + ” #thevideo”;

    $.ajax({

    url: ajaxurl,
    type:”GET”,
    beforeSend: function() {

    // animate the thumbnails and add ‘current’ class

    $port_holder.find(‘.current’).removeClass(‘current’);
    $currentitem.addClass(‘current’);

    // make sure the contact section is not visible anymore
    $j(‘.contacts a’).removeClass(‘active’);
    $j(‘#contacts’).fadeOut(300);

    $video_holder.find(‘#thevideo’).fadeOut(300, function() {
    this.remove();
    });

    //hide the menu
    $(‘.main-navigation’).fadeOut(300, function(){
    $(‘.loader’).show(400);
    });
    $(‘.portfolio_item’).addClass(‘closed’);

    },
    success: function( ) {

    $(‘.portfolio_item’).addClass(‘open’);

    $video_holder.load(newcontent, function() {
    //$video_holder.fitVids();
    $(‘.portfolio_item’).removeClass(‘closed’);
    $(‘.video_header’).fadeIn();
    //display a loader

    var theiframe = $(this).find(‘iframe’);
    theiframe.on(‘load’,function(){
    $(‘.loader’).hide(400);
    $(this).addClass(‘visible’);
    });

    //CloseHov();

    $(‘#thevideo’).on(‘touchstart click’, function(){
    $(‘.portfolio_item’).removeClass(‘open’);
    $video_holder.find(‘#thevideo’).fadeOut(300, function(){
    this.remove();
    $(‘.main-navigation’).fadeIn(300);
    $port_holder.find(‘.current’).removeClass(‘current’);
    });

    })
    });
    },
    complete: function(){

    }
    })

    })

    You’re sending an AJAX request to admin-ajax.php, but you’re not sending any information along with the request, and apparently haven’t added any code to even handle the request. So I’m not sure what you’re expecting to happen. I suggest you take another look at the documentation: https://codex.wordpress.org/AJAX_in_Plugins

    Moderator bcworkz

    (@bcworkz)

    anurag.deshmukh

    (@anuragdeshmukh)

    1. In order to use ajax with WordPress, you need to first use wp_enqueue_script() so javascript gets included on the page.

    
    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
    wp_localize_script( 'ajax-script', 'ajax_object',
                array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    

    2. Then you need to use below 2 hooks in order to add functionality and when we need to fire for both non logged in and logged-in users.

    
    add_action('wp_ajax_function_name', 'function_name');
    add_action('wp_ajax_nopriv_function_name', 'function_name');
    

    3.In your js file add code for calling ajax.

    
    jQuery('.class_name').click(function(){
    	
    	var str = {
    			'action': 'function_name',
    			'value1': 'value1 to pass',
    			'value2': 'value2 to pass',
    		};
    	jQuery.ajax({
    		    type: "POST",
    		    dataType: "html",
    		    url: ajax-script.ajaxurl,
    		    data: str,
    		    success: function(data){
    		        // Code after ajax success
    		    },
    		    error : function(jqXHR, textStatus, errorThrown) {
    		        $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
    		    }
    	});
    });
    

    4. Write your function in functions.php file.

    
    function function_name() {
    	// do stuffs whatever you wish to do in your function
    	$html = 'ajax';
    	echo $html; // please make sure echo your output here
    	wp_die(); // this is required to terminate immediately and return a proper response
    }
    

    Hope this helps.

    Thread Starter cmdcmdcmd

    (@cmdcmdcmd)

    Hi guys, I’ve made a new file to implement your process, although mine was working previously, still getting a 400 bad request error

    function add_js_scripts() {
    	wp_enqueue_script( 'script',  get_template_directory_uri() . '/js/ajax-nav.js', array('jquery'), '1.0', true );
    
    	// pass Ajax Url to script.js
    	wp_localize_script('script', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
    }
    add_action('wp_enqueue_scripts', 'add_js_scripts');
    add_action('wp_ajax_function_name', 'AjaxVid');
    add_action('wp_ajax_nopriv_function_name', 'AjaxVid');
    
    function AjaxVid(){ 
    	$post_id = $_POST['postid'];
    	$post_title = $_POST['title'];
    	$link = get_field("video_link", $post_id);
    	$_video_type = get_field("video_type", $post_id);
        $client = get_field("client", $post_id);
    	$director = get_field("director", $post_id);
    	
    	
    	echo '<div id="thevideo">';
        echo '<div class="video_header">';   
    	echo '<div class="item_title">';
    	echo $post_title;
    	echo '</div>';
    	if(!empty($client)):
    	echo '<div class="item_client">';
    	echo $client;
    	echo '</div>';
    	endif;
    	if(!empty($director)):
    	echo '<span class="sep"> | </span>';
    	echo '<div class="item_director">';
    	echo $director;
    	echo '</div>';
    	endif ;				
    	echo '</div>';
    	echo '<div class="video_inner">';
    	if(!empty($link)){
    		if($_video_type == "youtube"){
    		echo '<iframe class="mainvid" width="960" height="540" src="http://www.youtube.com/embed/'. $link .'?enablejsapi=1&iv_load_policy=3&showinfo=0&autoplay=1" frameborder="0" allowfullscreen="1" title="'. $post_title . '"></iframe>';
    						} elseif ($_video_type == "vimeo"){ 
    		echo '<iframe class="mainvid" width="960" height="540" src="http://player.vimeo.com/video/'. $link .'?title=0&wmode=opaque&api=1&autoplay=1" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
    						} elseif ($_video_type == "other"){
    		echo '<iframe src="<?php echo $link ;?>" width="960" height="540" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
    						} 	
    		} 
    		echo '</div></div>';
    	}
    

    and js

    (function($) {
    
    	$(document).on( 'touchstart click', '.portfolio_item', function( ) {
    		$port_holder = $('.portfolio_holder');	
    		$video_holder = $('#video_holder');
    		$currentitem = $(this);
    
    		post_id = $(this).attr('data-id');	
    		thetitle = $(this).attr('title');
    	
    
    		var str = {
    			'action': 'AjaxVid',
    			'postid': post_id,
    			'title': thetitle,
    		};
    		$.ajax({
    		    type: "POST",
    		    dataType: "html",
    		    url: ajaxurl,
    		    data: str,
    		    success: function(data){
    		        // Code after ajax success
    		    },
    		    error : function(jqXHR, textStatus, errorThrown) {
    		        $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
    		    }
    		});
    	});
    })(jQuery);
    anurag.deshmukh

    (@anuragdeshmukh)

    You need to replace wp_ajax_function_name with wp_ajax_AjaxVid and wp_ajax_nopriv_function_name with wp_ajax_nopriv_AjaxVid.
    So your new changes would look like :

    
    add_action('wp_ajax_AjaxVid', 'AjaxVid');
    add_action('wp_ajax_nopriv_AjaxVid', 'AjaxVid');
    

    in js part, url you need to pass is :

    url: script.ajaxurl,

    Thread Starter cmdcmdcmd

    (@cmdcmdcmd)

    Ok got it to work 🙂 Thank you very much for your help!!!

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

The topic ‘Admin-ajax.php 400 error’ is closed to new replies.