dongwu3747
2015-03-09 21:29
浏览 64
已采纳

调用AJAX时,Wordpress插件中的未定义索引

I created plugin for Wordpress and am now trying to learn Ajax in plugin.

When I call my function:

wp-admin/admin-ajax.php?action=my_action

I get this error:

Notice: Undefined index: whatever

This is my function

add_action( 'admin_footer', 'my_action_javascript' ); // Write our JS below here

function my_action_javascript() { ?>
    <script type="text/javascript" >
    jQuery(document).ready(function($) {
        var data = {
            'action': 'my_action',
            'whatever': 1234
        };
        // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
        $.post(ajaxurl, data, function(response) {
            alert('Got this from the server: ' + response);
        });
    });
    </script> <?php
}

and function call back

add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
    global $wpdb; // this is how you get access to the database
    $whatever = intval( $_POST['whatever'] );
    $whatever += 10;
        echo $whatever;
    wp_die(); // this is required to terminate immediately and return a proper response
}

What's causing this error and how can I fix it?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题