简体   繁体   English

从许多页面中包含的文件进行ajax调用

[英]Making an ajax call from a file that's included in many pages

I'm making a simple call from a file that's included in various pages. 我正在从各个页面中包含的文件中进行简单的调用。

$.post( "wp-content/themes/biagetti_studio/inc/HandleBackground.php", {color:color} );

The problem is that i thought that the call was relative to path of the file that include the js (all the files that are including the js are in the same directory), but i discovered that it's relative to the current path, so that if i'm in localhost/biagettistudio then the script calls localhost/biagettistudio/wp-content/themes/biagetti_studio/inc/HandleBackground.php but if i'm in localhost/biagettistudio/projects it calls localhost/biagettistudio/projects/wp-content/themes/biagetti_studio/inc/HandleBackground.php . 问题是我认为该调用是相对于包含js的文件的路径(所有包含js的文件都在同一目录中),但是我发现该调用相对于当前路径,因此如果我在localhost/biagettistudio那么脚本调用localhost/biagettistudio/wp-content/themes/biagetti_studio/inc/HandleBackground.php但是如果我在localhost/biagettistudio/projects则调用localhost/biagettistudio/projects/wp-content/themes/biagetti_studio/inc/HandleBackground.php

i know i could put an absolute path 我知道我可以走绝对的道路

$.post( "/wp-content/themes/biagetti_studio/inc/HandleBackground.php", {color:color} );

but this presents a problem if i have to port the site were the base directory is not the root. 但这提出了一个问题,如果我必须移植站点,而基本目录不是根目录。 What am i obviously missing here? 我显然在这里想念什么?

you could use 你可以用

<script type="text/javascript">
    <?php
    echo "var theme_dir ='" . get_theme_root() . ‘/’ . get_template() ."';";
    ?>
</script>

in footer.php (so it's available on every page) and you wouldn't have to worry about breaking stuff if you port the site. 在footer.php中(因此它在每个页面上都可用),如果您移植站点,则不必担心会破坏内容。

You can then call the script like this... 然后您可以像这样调用脚本...

$.post( theme_dir + "/biagetti_studio/inc/HandleBackground.php", {color:color} );

how about this: 这个怎么样:

var path = location.pathname.match(/(.*)\/wp-content\/.*/)[1];
$.post( path + "/wp-content/themes/biagetti_studio/inc/HandleBackground.php", {color:color} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM