简体   繁体   English

我如何在Prototype.js中使用Ajax将XML文件转换为XML(而不是字符串)?

[英]How do I get an XML file as XML (and not a string) with Ajax in Prototype.js?

This code is from Prototype.js . 这段代码来自Prototype.js I've looked at probably 20 different tutorials, and I can't figure out why this is not working. 我看了大概20种不同的教程,但我不知道为什么这不起作用。 The response I get is null. 我得到的响应为空。

new Ajax.Request(/path/to / xml / file.xml, {
   method: "get",
   contentType: "application/xml",
   onSuccess: function(transport) {
      alert(transport.responseXML);
   }
});

If I change the responseXML to responseText , then it alerts to me the XML file as a string. 如果将responseXML更改为responseText ,那么它将以字符串形式向我发出XML文件警报。 This is not a PHP page serving up XML, but an actual XML file, so I know it is not the response headers. 这不是提供XML的PHP​​页面,而是实际的XML文件,因此我知道它不是响应头。

If transport.responseXML is null but you have a value for transport.responseText then I believe it's because it's not a valid XML file. 如果transport.responseXML为null,但是您有一个transport.responseText值,那么我认为这是因为它不是有效的XML文件。

Edit: I just noticed that in our code here whenever we request an XML file we set the content type to 'text/xml'. 编辑:我只是注意到,在这里我们的代码中,每当我们请求XML文件时,我们都会将内容类型设置为'text / xml'。 I have no idea if that makes a difference or not. 我不知道这是否有所作为。

Just want to share my afternoon working on the issue with a NULL result for responseXML responses. 只想与我分享我在下午工作的下午, responseXML响应的结果为NULL。 My results were exactly as described in the question: responseText was filled with the XML file, responseXML was NULL. 我的结果与问题中所描述的完全相同:responseText充满了XML文件,responseXML为NULL。 As i was totally sure my file is in valid XML format, the error must be somewhere different. 由于我完全确定我的文件是有效的XML格式,因此错误必须在其他地方。

As mentioned in the Prototype v1.7 documentation, i set the content type to "application/xml" . Prototype v1.7文档中所述,我将内容类型设置为“ application / xml” The response sent was constantly "text/html", no matter what. 无论如何,发送的响应始终是“ text / html”。

To make it short, the problem i ran into was, that my XML file had the ending ".gpx" as it's a de facto standard for GPS coordinates. 简而言之,我遇到的问题是,我的XML文件以“ .gpx”结尾,因为它是GPS坐标的事实上的标准。 The mime-types collection of my local XAMPP Apache installation only foresees the endings "xml" and "xsl". 我的本地XAMPP Apache安装的mime-types集合仅预见了结尾“ xml”和“ xsl”。 After adding "gpx" and restarting the server the program ran smoothly as it's supposed to be. 添加“ gpx”并重新启动服务器后,程序按预期的那样顺利运行。

In my case, there are three solutions: 就我而言,有三种解决方案:

1) edit the "mime.types" file of Apache. 1)编辑Apache的“ mime.types”文件。 Using an XAMPP installation, you might find it under "C:\\xampp\\apache\\conf\\mime.types". 使用XAMPP安装,您可能会在“ C:\\ xampp \\ apache \\ conf \\ mime.types”下找到它。 Search for the "application/xml" record and change as follows: 搜索“ application / xml”记录,并进行如下更改:

application/xml xml xsl gpx

Don't forget to restart the server! 不要忘记重启服务器!

2) add the mime type in a .htaccess of the appropriate folder. 2)在相应文件夹的.htaccess中添加mime类型。 Open or create a .htaccess file and add following line: 打开或创建一个.htaccess文件,并添加以下行:

AddType application/xml xml xsl gpx

3) during upload process, change file type to "xml" instead of whatever you have 3)在上传过程中,将文件类型更改为“ xml”,而不用更改任何文件类型

Hope i safe some time to one of you guys. 希望我能安全地与你们中的一个人在一起。

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

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