Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress前端Ajax与wp_localize_脚本错误:未定义ajaxurl_Php_Jquery_Ajax_Wordpress_Frontend - Fatal编程技术网

Php Wordpress前端Ajax与wp_localize_脚本错误:未定义ajaxurl

Php Wordpress前端Ajax与wp_localize_脚本错误:未定义ajaxurl,php,jquery,ajax,wordpress,frontend,Php,Jquery,Ajax,Wordpress,Frontend,我正试图通过ajax在地图上创建一个wp主题的标记。 经过一番挣扎,我发现我不能使用任何php文件通过ajax获取数据,我必须使用admin-ajax.php文件 根据许多示例,这是我的代码 在functions.php中 add_action( 'wp_enqueue_scripts', 'add_frontend_ajax_javascript_file' ); function add_frontend_ajax_javascript_file() { wp_localize_scr

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

根据许多示例,这是我的代码

在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
}
js文件已加载并工作,它在ajax调用之前会执行其他操作,在这一行出现错误时会停止:

$.post({ 
        url:frontendajax.ajaxurl,
        {
            action: 'get_post_information',
            data: data
        },
        success: function(response) {
但我总是有同样的错误:

引用错误:未定义frontendajax.ajaxurl

我的错误在哪里

PS:我使用get_stylesheet_directory_uri(),因为我在子主题中。

来自:

重要!在使用wp_register_script()或wp_enqueue_script()注册要附加到的脚本后,必须调用wp_localize_script()

手柄必须相同:

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

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

admin_url('admin ajax.php') )); });
?>谢谢,我没有意识到我需要使用相同的手柄!为了使它工作,我还必须传递动作函数的名称和数据(我更新了我的问题)。脚本的位置是导致我出现问题的原因。这是正确的答案!宾!那完全是我的问题。我在函数中使用了wp_enqueue_script()和wp_localize_script(),但顺序错误。交换它们完全解决了我的问题。我真的建议你仔细阅读WP-Ajax文档,我知道乍一看有点混乱,在这里你可以找到一个真实世界的例子