douli1306 2018-11-08 21:41
浏览 89

Wordpress插件类设置Ajax

Im making a plugin using Class , I need to run some Ajax on the Front end. All scripts are queue up correctly. when i trigger the call, i get 400 error.

class CCYTFeatured {

public function __construct(){
        add_action( 'wp_enqueue_scripts', array($this, 'cc_yt_scripts' ));
        add_action( 'wp_ajax_cc_get_featured_yt', array( $this, 'cc_get_featured_yt' ) );
        add_action( 'wp_ajax_nopriv_cc_get_featured_yt', array( $this, 'cc_get_featured_yt' ) );

} 
public function cc_yt_scripts() {

    // JAVASCRIPT
    wp_register_script( 'cc_yt_script',
        plugins_url( '/js/cc_yt.js', __FILE__ ),
        array('jquery'),
        cc_yt_version(),
        true
    );
    wp_localize_script(
        'cc_yt_script',
        'cc_yt_ajax',
        array(
            'ajax_url' => admin_url( 'admin-ajax.php' )
        )
    );
    wp_enqueue_script('cc_yt_script');

}
public function cc_get_featured_yt(){
    echo 'SUCCESS!';
    die();
}

My ajax call is:

function start_yt(){
jQuery('#cc_yt_light_wrap').show();
// REGISTER NEW ENTRY USING AJAX
jQuery.ajax({
    url: cc_yt_ajax.ajax_url,
    type: 'POST',
    data: {
        action : 'cc_get_featured_yt',
    },
    async: true,
    success: function(response) {
        console.log(response);
    }
});
}

Thanks for your help! X)

  • 写回答

2条回答 默认 最新

  • dongyang7152 2018-11-10 14:55
    关注

    Try this: Change the method name from cc_get_featured_yt to get_featured, and change the method name everywhere in your class. and then in the ajaxcall you put this code:

    data: {
        action : 'featured_yt',
    }
    

    I think the get in the methodname is a keyword. This worked for me.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭