douwu3763 2014-07-28 16:43
浏览 44
已采纳

需要帮助发现wordpress中的jQuery或php中的错误

I'm having trouble trying to figure out which part of my code is wrong, taking into account there are several languages involved.

Firstly I enqueue the script that I am going to run and then use wp_localize_script in the plugin main function file as follows:

// All scripts

add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts' );

function theme_enqueue_scripts() {

  // Enqueue and Localize AJAX JavaScript Functions File 
  wp_enqueue_script( 'ajax-categories-js', plugins_url( 'events-calendar-manager/inc/js/js.js' ), array('jquery'));
  wp_localize_script( 'ajax-categoreis-js', 'ajax_object_1', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );


}

The jQuery script looks like this:

// JavaScript Document

jQuery(document).ready(function($){

        console.log('Started Jquery');

        var year='', month='', category='';

     $('#next_link').click(function(e){
         e.preventDefault();

         var $aid =$('#next_link');
         year =$aid.data('year');
         month =$aid.data('month');
         category =$aid.data('category');
         console.log('category: ' + category);

     });

    $('#previous_link').click(function(e){
         e.preventDefault();

         var $dai =$('#previous_link');
         year =$dai.data('year');
         month =$dai.data('month');
         category =$dai.data('category');
         console.log('category: ' + category);

     });

    console.log('category: ' + category);


    $.ajax({
         cache: false,
         timeout: 8000,
         type: 'POST',
         data: {action: 'get_post_filter_by_date_and_category', year : year, month: month, category: category},
         url: ajax_object_1.ajaxurl,
         success: function(data) {},
         error: function() {}
    });

});

However when I run it I get a mistake in the console saying : Uncaught ReferenceError: ajax_object_1 is not defined . This is weird as the object should be passed on using the wp_localize_script function.

What creates this problem?

  • 写回答

2条回答

  • dqajyxqem115006813 2014-07-28 16:50
    关注

    You have a typo here

    wp_enqueue_script( 'ajax-categories-js' ...
    wp_localize_script( 'ajax-categoreis-js', ...
    

    See ajax-categories-js !== ajax-categoreis-js

    wp_localize_script( 'ajax-categories-js', 'ajax_object_1', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程