Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.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/0/search/2.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
Javascript 理解wp_本地化_脚本函数背后的概念存在问题_Javascript_Php_Ajax_Wordpress_Localization - Fatal编程技术网

Javascript 理解wp_本地化_脚本函数背后的概念存在问题

Javascript 理解wp_本地化_脚本函数背后的概念存在问题,javascript,php,ajax,wordpress,localization,Javascript,Php,Ajax,Wordpress,Localization,我目前正在自学如何在Wordpress中使用AJAX。为了获得AJAX管理员URL,我使用Wordpress提供的wp_localize_脚本函数()将URL传递给数组中的Javascript文件。在我的JS文件中,我可以访问我“附加”到JS的数组。虽然这很好,但我很难理解它背后的概念 <?php // Register the script wp_register_script( 'some_handle', 'path/to/myscript.js' ); // Localize

我目前正在自学如何在Wordpress中使用AJAX。为了获得AJAX管理员URL,我使用Wordpress提供的wp_localize_脚本函数()将URL传递给数组中的Javascript文件。在我的JS文件中,我可以访问我“附加”到JS的数组。虽然这很好,但我很难理解它背后的概念

<?php

// Register the script
wp_register_script( 'some_handle', 'path/to/myscript.js' );

// Localize the script with new data
$translation_array = array(
    'ajax_url' => admin_url( 'admin-ajax.php' )
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );

// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );

好吧,经过进一步的研究,我发现了——我只是看错了地方。数据实际上并没有写入任何特定的Javascript文件(或神奇地附加到其中)

它只是在DOM中作为HTML
标记内的对象打印出来,如下所示:

<script type="text/javascript">
/* <![CDATA[ */
var ajaxUrl = {"ajaxurl":"http://*******************/admin-ajax.php"};
/* ]]> */
</script> 

/*  */
从这里可以访问所有JS文件

<script type="text/javascript">
/* <![CDATA[ */
var ajaxUrl = {"ajaxurl":"http://*******************/admin-ajax.php"};
/* ]]> */
</script>