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

我在试图找出我的代码的哪一部分是错误的时候遇到了麻烦,考虑到有几种语言 参与。</ p>

首先,我将要运行的脚本排入队列,然后在插件主函数文件中使用 wp_localize_script </ code>,如下所示:</ p>

  //所有脚本

add_action('wp_enqueue_scripts','theme_enqueue_scripts');

function theme_enqueue_scripts(){

// //将AJAX JavaScript函数文件排队并本地化
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'=&gt; admin_url('admin_ajax.php')));

}
</ code> </ pre>

jQuery脚本看起来像 这个:</ p>

  // JavaScript Document 

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

console.log('Started Jquery'); \ n
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){},
错误:function(){}
});

});
</ code> </ pre>

然而</ strong>时 我运行它在控制台中出现错误: Uncaught ReferenceError:ajax_object_1未定义</ code>。 这很奇怪,因为对象应该使用wp_localize_script函数传递。</ p>

是什么造成了这个问题?</ p>
</ div>

展开原文

原文

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个回答



这里有一个拼写错误</ p>

  wp_enqueue_script('ajax-categories-js'  ... 
wp_localize_script('ajax-categoreis-js',...
</ code> </ pre>

请参阅 ajax-categories-js!== ajax-categoreis- js </ code> </ p>

  wp_localize_script('ajax-categories-js','ajax_object_1',array('ajaxurl'=&gt; admin_url('admin_ajax.php'))  ); 
</ code> </ pre>
</ div>

展开原文

原文

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' ) ) );

dtuct88226
dtuct88226 虽然现在还有其他jQuery错误:(
大约 6 年之前 回复
dqkv0603
dqkv0603 谢谢@Rahil Wazir :)
大约 6 年之前 回复



不知道关于WP的nada,但看起来你有一个拼写错误:</ p>

  wp_localize_script('ajax-categoreis-js','ajax_object_1',array('ajaxurl'=&gt; admin_url('admin_ajax.php'))); 
</ code> </ pre>
\ n

你可能不是故意写 ...'ajax-categoreis-js'... </ code>但 ...'ajax-categories-js'... < / code> </ p>
</ div>

展开原文

原文

Don't know nada about WP, but it looks like you have a typo here:

  wp_localize_script( 'ajax-categoreis-js', 'ajax_object_1', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );

You probably didn't mean to write ...'ajax-categoreis-js'... but ...'ajax-categories-js'...

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问