Title: Admin-ajax.php 400 error
Last modified: March 9, 2018

---

# Admin-ajax.php 400 error

 *  [cmdcmdcmd](https://wordpress.org/support/users/cmdcmdcmd/)
 * (@cmdcmdcmd)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/)
 * 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](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fadmin-ajax-php-400-error%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  [Jacob Peattie](https://wordpress.org/support/users/jakept/)
 * (@jakept)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/#post-10057209)
 * Have you added a hook into the `wp_ajax_` hooks, and defined an `action` in the
   AJAX `data`?
 *  Thread Starter [cmdcmdcmd](https://wordpress.org/support/users/cmdcmdcmd/)
 * (@cmdcmdcmd)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/#post-10057264)
 * 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(){
 *  }
    })
 *  })
 *  [Jacob Peattie](https://wordpress.org/support/users/jakept/)
 * (@jakept)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/#post-10057685)
 * 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](https://codex.wordpress.org/AJAX_in_Plugins)
    -  This reply was modified 8 years, 1 month ago by [Jacob Peattie](https://wordpress.org/support/users/jakept/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/#post-10058702)
 * Another Ajax for WP resource FWIW: [https://developer.wordpress.org/plugins/javascript/ajax/](https://developer.wordpress.org/plugins/javascript/ajax/)
 *  [anurag.deshmukh](https://wordpress.org/support/users/anuragdeshmukh/)
 * (@anuragdeshmukh)
 * [8 years ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/#post-10072995)
 * 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](https://wordpress.org/support/users/cmdcmdcmd/)
 * (@cmdcmdcmd)
 * [8 years ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/#post-10079719)
 * 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](https://wordpress.org/support/users/anuragdeshmukh/)
 * (@anuragdeshmukh)
 * [8 years ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/#post-10081163)
 * 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](https://wordpress.org/support/users/cmdcmdcmd/)
 * (@cmdcmdcmd)
 * [8 years ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/#post-10081744)
 * 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.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 8 replies
 * 4 participants
 * Last reply from: [cmdcmdcmd](https://wordpress.org/support/users/cmdcmdcmd/)
 * Last activity: [8 years ago](https://wordpress.org/support/topic/admin-ajax-php-400-error/#post-10081744)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
