dongzhang0243 2017-02-09 08:57
浏览 95
已采纳

WordPress中的Qtip和ajax - 操作不起作用

I'm trying to integrate the QTip jquery tooltip in my Wordpress site, using Ajax to trigger a tooltip with some information pulled from a custom database table.

I've added the enque and action stuff to my function.php:

function enqueue_scripts_styles_init() {
  wp_enqueue_style('qtip', 'http://www.my-site.com/wp-content/themes/my-theme/qtip/jquery.qtip.min.css', null, false, false);
  wp_enqueue_script('qtip', 'http://www.my-site.com/wp-content/themes/my-theme/qtip/jquery.qtip.min.js', array('jquery'), false, true);
  wp_enqueue_script('qtipCall', 'http://www.my-site.com/wp-content/themes/my-theme/qtip/qtipcall.js', array('jquery', 'qtip'), false, true);
  wp_localize_script( 'qtipCall', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('init', 'enqueue_scripts_styles_init');

function ajax_action_stuff() {
  global $wpdb;
  $hero = $_POST['who'];
  $results = $wpdb->get_row( $wpdb->prepare ('SELECT * FROM wp_herotable WHERE Name = %s', $hero) );
  $hero_bio = $results->Bio;
  echo $hero_bio;
  wp_die();
}
add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' );
add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' );

This is my qtipcall.js:

jQuery(document).ready(function( $ ) {
$('[tip-hero').each(function() {
    $(this).qtip({
        content: {
            text: function(event, api) {
                $.ajax({
                url: ajax_object.ajaxurl,
                type: 'POST',
                action: 'ajax_action',
                data: {who: $(this).attr("tip-hero")},
            })
                .then(function(content) {
                    // Set the tooltip content upon successful retrieval
                    api.set('content.text', content);
                }, function(xhr, status, error) {
                    // Upon failure... set the tooltip content to error
                    api.set('content.text', status + ': ' + error);
                });

                return 'Loading...'; // Set some initial text
            }

        },

        style: { 
        classes: 'qtip-dark qtip-rounded qtip-shadow'
        }
    });
});

});

Finally, my page:

<a hfer="#" tip-hero="Joan of Arc">Joan of Arc</a>

The result is a cool tooltip with a 0 in it! I've checked the admin-ajax.php and the zero is coming from:

// Require an action parameter
if ( empty( $_REQUEST['action'] ) )
die( '0' );

So I think there is a problem with my action call but I really can't understand what it is.

Any help will be appreciated.

Thanks.

  • 写回答

1条回答 默认 最新

  • dongshang6790 2017-02-09 09:02
    关注

    Change your ajax to send the action value:

       data: {who: $(this).attr("tip-hero"),action: 'ajax_action'},
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#r语言#rda分析作图的问题,请各位专家解答!
  • ¥50 找回 股票行情系统的资源问题
  • ¥15 gwas 分析-plink 检查人口分层出现下面问题
  • ¥15 关于#matlab#的问题:需要 MATLAB 运行程序,对比四个多址通信方式(TDMA,FDMA,CDMA,OFDMA)的抗噪声性能
  • ¥15 有没有复现过PFENet的,如何解决?
  • ¥15 vue3使用swiper轮播图循环失败
  • ¥15 FDMA,OFDMA,TDMA 和 CDMA 系统容量对比仿真
  • ¥15 图神经网络的入侵检测
  • ¥15 工业数据采集技术+存储架构推荐
  • ¥15 GPT-SOVITS如何训练少数民族语言支持合成语音