Following the directions here http://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Administration_Side

I wrote this code:

EDITED TO INCLUDE DIE(), STILL NOT WORKING

function my_button() {
echo
"<script type="text/javascript">
function ajaxRequest(){
    jQuery(document).ready(function(jQuery) {
        var sendData = {};
        sendData['action'] = 'my_action';
        sendData['external_id'] = '$postID';
        sendData['title'] = '$post_title';
        sendData['content'] = '$post_content';
        jQuery.ajax({
            type: 'POST',
            url: 'http://lvh.me:3000/s/wp',
            xhrFields: {
                withCredentials: true
            },
            headers: {'X-Requested-With': 'XMLHttpRequest'},
            data: sendData,
            error: function(jqXHR){
                console.log(jqXHR.responseText);
            },
            success: function(data){
                window.open(data['link']);
            }
        });
    })
};
</script>
<input type="button" onclick='ajaxRequest()' value="Send" />";
}

add_action( 'dbx_post_sidebar', my_button);

add_action('wp_ajax_my_action', my_action_callback);

function my_action_callback() {
  global $wpdb; // this is how you get access to the database
  $api_key = $_POST['api_key'];
  $user = wp_get_current_user();
  $user_id = $user->ID;
  add_user_meta($user_id, 'my_api_key', $api_key);
  die();
}

I know that’s a ton of code, but I just can’t figure out where the error is.

Thanks a ton.

3 Answers
3

Your url should be pointing to admin-ajax.php

echo admin_url('admin-ajax.php');

Leave a Reply

Your email address will not be published. Required fields are marked *