title: Ajax post in wordpress
body: <p>I am trying to submit a form using post using ajax method in wordpress by following this link.
  <a href="http://wp.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/" rel="nofollow">http://wp.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/</a>
  I have got stuck at a point a ajax call goes to 
  <a href="http://www.yourwpdirectory.com/wp-admin/admin-ajax.php" rel="nofollow">http://www.yourwpdirectory.com/wp-admin/admin-ajax.php</a> 
  but it does not return me anything thereby redirecting me to the action call using a browser request.</p>
  
  <p>Can anyone provide me a  simple way in which I can make a ajax post in wordpress.</p>
  
  <hr>
  
  <p>I am updating the plugin code as per the link given above:</p>
  
  <pre><code>  &lt;?php
  
  
  /*
   * Plugin Name: My plugin
   * Description: test plugin
   * Version: 1.0
   * Author: Interview Coordinator 
   */
  
  
  
  
  
  
  
  add_action("wp_ajax_my_user_vote", "my_user_vote");
  add_action("wp_ajax_nopriv_my_user_vote", "my_must_login");
  
  function my_user_vote() {
  
     if ( !wp_verify_nonce( $_REQUEST['nonce'], "my_user_vote_nonce")) {
        exit("No naughty business please");
     }   
  
     $vote_count = get_post_meta($_REQUEST["post_id"], "votes", true);
     $vote_count = ($vote_count == '') ? 0 : $vote_count;
     $new_vote_count = $vote_count + 1;
  
     $vote = update_post_meta($_REQUEST["post_id"], "votes", $new_vote_count);
  
     if($vote === false) {
        $result['type'] = "error";
        $result['vote_count'] = $vote_count;
     }
     else {
        $result['type'] = "success";
        $result['vote_count'] = $new_vote_count;
     }
  
     if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $result = json_encode($result);
        echo $result;
     }
     else {
        header("Location: ".$_SERVER["HTTP_REFERER"]);
     }
  
     die();
  
  }
  
  function my_must_login() {
     echo "You must log in to vote";
     die();
  }
  
  add_action( 'init', 'my_script_enqueuer' );
  
  function my_script_enqueuer() {
     wp_register_script( "my_voter_script", WP_PLUGIN_URL.'/my_plugin/my_voter_script.js', array('jquery') );
     wp_localize_script( 'my_voter_script', 'myAjax', array( 'ajaxurl' =&gt; admin_url( 'admin-ajax.php' )));        
  
     wp_enqueue_script( 'jquery' );
     wp_enqueue_script( 'my_voter_script' );
  
  }
  
  
     $votes = get_post_meta($post-&gt;ID, "votes", true);
     $votes = ($votes == "") ? 0 : $votes;
  ?&gt;
  This post has &lt;div id='vote_counter'&gt;&lt;?php echo $votes ?&gt;&lt;/div&gt; votes&lt;br&gt;
  
  &lt;?php
     $nonce = wp_create_nonce("my_user_vote_nonce");
  
    // $nonce = 123;
      $link = admin_url('admin-ajax.php?action=my_user_vote&amp;post_id='.$post-&gt;ID.'&amp;nonce='.$nonce);
  //     echo '&lt;a class="user_vote" data-nonce="' . $nonce . '" data-post_id="' . $post-&gt;ID . '" href="' . $link . '"&gt;vote for this article&lt;/a&gt;';
  
   echo '&lt;a class="user_vote" data-nonce="' . $nonce . '" data-post_id="' . $post-&gt;ID . '" href="' . $link . '"&gt;vote for this article&lt;/a&gt;';
  
  
      ?&gt;
  </code></pre>
  
  <hr>
  
  <p>my_voter_script.js</p>
  
  <pre><code>jQuery(document).ready( function() {
  
     jQuery(".user_vote").click( function() {
        post_id = jQuery(this).attr("data-post_id")
        nonce = jQuery(this).attr("data-nonce")
  
        jQuery.ajax({
           type : "post",
           dataType : "json",
           url : myAjax.ajaxurl,
           data : {action: "my_user_vote", post_id : post_id, nonce: nonce},
           success: function(response) {
              if(response.type == "success") {
                 jQuery("#vote_counter").html(response.vote_count)
              }
              else {
                 alert("Your vote could not be added")
              }
           }
        })   
  
     })
  
  })
  </code></pre>