在中。在下面的js脚本中,当包含页面脱离站点的根目录时,ajaxPath引用将中断。是否有一种变通方法使此路径成为绝对路径?
jQuery(document).ready(function($){
initContentEditable();
var dir = ContentEditableSettings.content_editable_url;
var ajaxPath = \'wp-admin/admin-ajax.php\'; //THIS RELATIVE PATH BREAKS OUTSIDE ROOT DIRECTORY
function initContentEditable(){
$(\'.contenteditable.furniture\').blur(updateContentEditable);
$(\'.contenteditable.custom\').blur(updateContentEditableCustom);
$(\'.contenteditable.title\').blur(updateContentEditableTitle);
$(\'.contenteditable\').each(function(){
var parent = $(this).parent();
var display = parent.css(\'display\');
$(this).css(\'display\', display);
});
}
function updateContentEditableTitle(e){
e.preventDefault();
var span = $(this);
var data = new Object();
data.pid = span.attr(\'data-pid\');
data.title = span.find(\'.saver\').text();
data.action = \'ce_update_title\';
data.security = ContentEditableSettings.nonce;
$.post(ajaxPath, data, onContentSaved); //AJAXPATH BREAKS AS RELATIVE REF
}
最合适的回答,由SO网友:Joseph Leedy 整理而成
只要你wp-admin
目录仍然位于站点的根目录下,只需添加正斜杠即可(/
) 到URI。或者,您可以使用wp_localize_script()
要传入站点的URL,或者如果您使用WordPress的AJAX功能,则ajaxurl
JS变量。
var ajaxPath = \'/wp-admin/admin-ajax.php\';
或
<?php
wp_enqueue_script( \'some_handle\' );
$translation_array = array( \'ajaxPath\' => admin_url( \'admin-ajax.php\' ) );
wp_localize_script( \'some_handle\', \'object_name\', $translation_array );
?>