从服务器(PHP脚本)获取特定xml文件的链接,并将其加载到javascript中 [英] Get the link of an specific xml file from the server (php script) and load it in javascript
问题描述
在网站上,我正在创建一个可以上载和下载文件的用户,并且创建了一个包含操作信息的xml日志.该网站上有一个页面,其中创建了读取xml的日志xml表.
In the web site I am creating a user can upload and download files and I create an xml log with operation informations. In the website there is a page where a table of the log xml is created reading the xml.
当它全部放在硬盘中时,它工作正常,但是现在我开始将其托管在服务器上,我不知道如何使它工作.
It worked fine when it was all in my hard disk but now that I started hosting it on a server I don't know how to make it work.
在本地计算机上,xml请求是
In local computer the xml request was
xmlhttp.open("GET","file.xml",false);
现在在真实的网站中,我需要对php脚本进行ajax调用,以获取我的个人xml文件的路径.
Now in the real web site I need to make an ajax call to a php script to geth the path of my personal xml file.
第一个问题:我需要从php脚本输出哪种地址?
First question: what kind of address i need to output from the php script?
http://mywebsite/folder1/folder2/log_username.xml
是这样吗?
通过ajax调用,我将地址保存在一个变量中,并且xml请求变为:
With the ajax call I save the address in a variable and the xml request became:
xmlhttp.open("GET",xml_address_variable,false);
问题是未加载XML.问题出在哪里?
The problem is that the XML isn't loaded. Where is the problem?
编辑过的:现在,我在javascript代码中写了一个特定用户的实验路径,它可以工作,但是如果我将变量(link_xml)放在AJAX调用应保存路径的位置,它将不起作用,因此这是我的代码的关键部分:
EDITED: now that i wrote in the javascript code the path of a specific user for experiment it worked, but if I put the variable (link_xml) where the AJAX call should save the path it doesn't work so this is the critical part of my code:
var link_xml = askXMLaddress();
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",link_xml,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
推荐答案
要让ajax加载它,您需要来自文档根目录的文件路径.所以如果你想
for ajax to load it you'd need a file path from your doc-root. so if you want
http://mywebsite/folder1/folder2/log_username.xml
所有JS需求都是
../folder1/folder2/log_username.xml
然后您可以像这样使用jquery.load()
then you can use jquery.load() like so
var path = <?php echo "../folder1/folder2/log_username.xml"; ?>;
$( "#username" ).load(path);
现在,最好使用另一个ajax请求,而不要使用"php echo ..."之类的东西.但是这将取决于您的用例.
now instead of that 'php echo...' stuff you'd probably do better to use another ajax request. BUT that will depend on your use case.
这篇关于从服务器(PHP脚本)获取特定xml文件的链接,并将其加载到javascript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!