douhui9380
2015-04-18 23:57
浏览 142

带有wp_localize_script的Wordpress前端Ajax错误:未定义ajaxurl

I'm trying to create markers on a map via ajax on a wp theme. After some struggle I found out that I can't use any php file to get data via ajax, I have to use the admin-ajax.php file.

Accordingly to many examples, this is my code

in functions.php

add_action( 'wp_enqueue_scripts', 'add_frontend_ajax_javascript_file' );
function add_frontend_ajax_javascript_file()
{
   wp_localize_script( 'frontend_ajax', 'frontendajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
    wp_enqueue_script( 'ajax_custom_script',  get_stylesheet_directory_uri() . '/includes/ajax-javascript.js', array('jquery') );

}

add_action( 'wp_ajax_get_post_information', 'get_post_information' );
add_action( 'wp_ajax_nopriv_get_post_information', 'get_post_information' );


function get_post_information() 
{ 

$get_this= $_GET['this'];
$get_that= $_GET['that'];

...my select...

echo json formatted data
}

The js file is loaded and working, it does other stuff before the ajax call, where it stops for an error in this line:

$.post({ 
        url:frontendajax.ajaxurl,
        {
            action: 'get_post_information',
            data: data
        },
        success: function(response) {

But I always Have the same error:

Reference Error: frontendajax.ajaxurl is not defined

where is my error?

PS: I use get_stylesheet_directory_uri() because I am in a child theme.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试通过wp主题上的ajax在地图上创建标记。 经过一番挣扎,我发现了 我不能使用任何php文件通过ajax获取数据,我必须使用admin-ajax.php文件。

相应的许多例子,这是我的代码

  add_action('wp_enqueue_scripts','add_frontend_ajax_javascript_file'); 
 
 
 
函数add_frontend_ajax_javascript_file()
 
 
 \ {
 
 
 
 
wp_localize_script('  frontend_ajax','frontendajax',array('ajaxurl'=> admin_url('admin-ajax.php'))); 
 wp_enqueue_script('ajax_custom_script',get_stylesheet_directory_uri()。'/ include / ajax-javascript.js'  ,array('jquery')); 
 
} 
 
add_action('wp_ajax_get_post_information','get_post_information'); 
add_action('wp_ajax_nopriv_get_post_information','get_post_information'); 
 
 
函数get_post_information()\  n {
 
 $ get_this = $ _GET ['this']; 
 $ get_that = $ _GET ['that']; 
 
 ...我的选择... 
 
echo json格式化d  ata 
} 
   
 
 

js文件已加载并正常工作,它在ajax调用之前执行其他操作,在此行中出现错误:

  $ .post({
 url:frontendajax.ajaxurl,
 {
 action:'get_post_information',
 data:data 
},
 success:function  (响应){
   
 
 

但我总是有同样的错误:

参考错误:frontendajax.ajaxurl是 未定义

我的错误在哪里?

PS:我使用get_stylesheet_directory_uri()因为我处于子主题中。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dougeqiang1619 2015-04-19 01:09
    已采纳

    From wp_localize_script docs:

    IMPORTANT! wp_localize_script() MUST be called after the script it's being attached to has been registered using wp_register_script() or wp_enqueue_script().

    And the handle must be the same:

    wp_enqueue_script( 'ajax_custom_script',  get_stylesheet_directory_uri() . '/includes/ajax-javascript.js', array('jquery') );
    wp_localize_script( 'ajax_custom_script', 'frontendajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
    
    打赏 评论
  • dpge74512 2017-07-14 07:24

    I'm trying to create markers on a map via ajax on a wp theme. After some struggle I found out that I can't use any php file to get data via ajax, I have to use the admin-ajax.php file.

    admin_url( 'admin-ajax.php' ) )); }); ?>
    打赏 评论

相关推荐 更多相似问题