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'},
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同