我为自己制定了一个个人框架。我在我的网站include
的每个网页中添加了这个ajax脚本,在每个网页中都添加了一个PHP文件。
我把这个脚本放在我的add.php文件中:
function demo(str)
{
if (str.length==0){ return; }
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","bin.php?tid="+str,true);
xmlhttp.send();
}
demo('astring');
问题是,这个ajax称为PHP文件(bin.php)在我的文件夹root/a/bin.php
中。
虽然我的网页有很多文件夹和子文件夹,如root/a
、root/b
、root/a/a1
、root/b/b1/b2
等等。
我不能单独在每个页面上添加这个ajax调用脚本。这就是为什么我在每个网页上都包含了一个PHP文件,所以以后我只需要修改这个PHP文件,就可以对每个网页进行修改。
这个ajax只在一些网页上工作(由于ajax称为文件的路径问题),请您给我建议一下这个ajax脚本的解决方案,这样它就可以在每个网页上工作了吗?
发布于 2013-12-19 02:09:14
可以使用此代码生成绝对路径。
var url = location.protocol + '//' + document.domain + '/your_path/' + "bin.php?tid="+str;
xmlhttp.open("GET",url,true);
发布于 2013-12-19 02:14:45
使用相对站点路径:
function demo(str)
{
if (str.length==0){ return; }
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/a/bin.php?tid="+str,true); // links to http(s)://<domain>/a/bin.....
xmlhttp.send();
}
demo('astring');
https://stackoverflow.com/questions/20678434
复制