0

In the latest version of wp I created a theme, on the index.php page I have the following HTML:

<p><input type="hidden" name="GreetingAll" id="GreetingAll" value="Hello Everyone!" /> <input type="submit" id="PleasePushMe" /></p>  

<div id="test-div1"></div> 

My theme functions.php file, I added the following functions:

function add_myjavascript(){
  wp_enqueue_script( 'js', get_template_directory_uri().'/lib/js/js.js', array( 'jquery' ));
  //wp_localize_script( 'js', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'add_myjavascript' );

function MyAjaxFunction(){
  //get the data from ajax() call
   $GreetingAll = $_POST['GreetingAll '];
   $results = "<h2>".$GreetingAll."</h2>";
  // Return the String
   die($results);
}
// creating Ajax call for WordPress
add_action( 'wp_ajax_nopriv_MyAjaxFunction', 'MyAjaxFunction' );
add_action( 'wp_ajax_MyAjaxFunction', 'MyAjaxFunction' );

My JavaScript js.js:

jQuery(document).ready(function() {  
  var GreetingAll = jQuery("#GreetingAll").val();  
jQuery("#PleasePushMe").click(function(){ 

jQuery.ajax({  
  type: 'POST',  
  url: 'http://localhost/brenda/wordpress/wp-admin/admin-ajax.php',
  data: {  
  action: 'MyAjaxFunction',  
  GreetingAll: GreetingAll,  
  },  
  success: function(data, textStatus, XMLHttpRequest){ 
  alert(data);
  jQuery("#test-div1").html('');  
  jQuery("#test-div1").append(data);  
  },  
  error: function(MLHttpRequest, textStatus, errorThrown){  
  alert(errorThrown);  
  }  
  });  
  });  
  });  

My Ajax request works fine:

---------------
Request Method:POST
Status Code:200 OK
Form Data
action:MyAjaxFunction
GreetingAll:Hello Everyone!
---------------

Inside my admin-ajax.php the do_action fires. I echoed out a message in plugin.php to test to see if it was firing. However my function MyAjaxFunction() does not get executed. Note the add_actions are carried out, I tested that. My output into the target test-div1 is zero, which is the default status, see below.

if ( is_user_logged_in())
{
    do_action( 'wp_ajax_' . $_REQUEST['action'] ); // Authenticated actions
}
else
{
    do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ); // Non-admin actions
}
 //Default status
die('0');
brasofilo
  • 25,496
  • 15
  • 91
  • 179
user2197774
  • 453
  • 1
  • 6
  • 20
  • I got the above code to work in the theme "Twenty Thirteen", on a page. I am guessing, i am missing something from my theme. – user2197774 Oct 08 '13 at 00:54
  • A [working example](http://stackoverflow.com/questions/13498959/how-to-use-ajax-in-a-wordpress-shortcode/13614297) – brasofilo Oct 08 '13 at 00:58

1 Answers1

0

Content-Type - application/json

in file /wp-admin/admin-ajax.php

test send data ($_POST, $_REQUEST...)

if necessary, apply a fix

$headers = getallheaders();
if (strpos($headers['Content-Type'], 'application/json') !== false) {
        $input = json_decode(file_get_contents("php://input"), true); 
        $_REQUEST['action'] = $input['action'];
}

inserted after

/** Load WordPress Bootstrap */
require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );

here ...